with zipfile.ZipFile('../data/jaychou_lyrics.txt.zip', 'r') as zin:
zin.extractall('../data/')
# 将 .txt.zip 解压为 .txt
with open('../data/jaychou_lyrics.txt') as f:
f.read()
1. zipfile 下的 ZipFile 类
f = zipfile.ZipFile(filename)
with zipfile.ZipFile(filename) as f:
ZipFile 对象的基本成员函数:
- f.namelist() ⇒ 由 names 构成的 list;
- f.read(f.namelist()[0])
2. 一个 zip 文件的解析
# 使用 tensorflow 下的相关接口
with parse_data(filename):
with zipfile.ZipFile(filename) as f:
data = tf.compat.as_str(f.read(f.namelist()[0])).split()
return data