我有以下生成损坏的 gzip 文件的最小代码。
import io
import csv
import gzip
items = [{"a": "1", "b": "2"}, {"a": "3", "b": "4"}]
buffer = io.BytesIO()
with gzip.open(buffer, mode="wt", newline='', encoding="utf-8") as compressed:
#compressed.write(". ")
writer = csv.DictWriter(compressed, fieldnames=items[0].keys(), quoting=csv.QUOTE_NONNUMERIC)
writer.writeheader()
writer.writerows(items)
buffer.seek(0)
with open("temp.csv.gzip", "wb") as f:
f.write(buffer.read())
出于某种原因,如果我取消注释
#compressed.write(". ")
的行,它会生成一个有效文件。那里有空格的任何类型的字符串也可以。是什么导致了这种行为?