【发布时间】:2020-11-28 12:22:22
【问题描述】:
我有一个列表,假设它是:
sentcorpus = [["hello", "how", "are", "you", "?"], ["hello", "I", "'m", "fine"]]
我想用gzip格式保存:
import gzip
import json
with gzip.open('corpus.json.gz', 'wb') as fileh:
fileh.write(json.dumps(sentcorpus).encode("utf8"))
那么这样读回来是合乎逻辑的:
with gzip.open('wbec_corpus.json.gz', 'rb') as fileh:
sentcorpus = json.load(fileh.read().decode("utf8"))
但没有:
AttributeError: 'str' object has no attribute 'read'
相反,这个可行:
with gzip.open('wbec_corpus.json.gz', 'rb') as fileh:
sentcorpus = json.load(fileh)
为什么fileh 是字符串而不是文件句柄?
【问题讨论】:
标签: python string gzip binaryfiles binary-data