【发布时间】:2018-03-07 08:48:39
【问题描述】:
我想在一个文件夹中解压缩子文件夹中有多个 gzfile。它工作正常,但我想删除的每个文件的开头都有一个 BOM 签名。我检查了其他问题,例如Removing BOM from gzip'ed CSV in Python 或Convert UTF-8 with BOM to UTF-8 with no BOM in Python,但它似乎不起作用。我在 Windows 上的 Pycharm 中使用 Python 3.6。
这是我没有尝试的代码:
import gzip
import pickle
import glob
def save_object(obj, filename):
with open(filename, 'wb') as output: # Overwrites any existing file.
pickle.dump(obj, output, pickle.HIGHEST_PROTOCOL)
output_path = 'path_out'
i = 1
for filename in glob.iglob(
'path_in/**/*.gz', recursive=True):
print(filename)
with gzip.open(filename, 'rb') as f:
file_content = f.read()
new_file = output_path + "z" + str(i) + ".txt"
save_object(file_content, new_file)
f.close()
i += 1
现在,如果我将file_content = f.read() 替换为file_content = csv.reader(f.read().decode('utf-8-sig').encode('utf-8').splitlines()),则使用Removing BOM from gzip'ed CSV in Python 中定义的逻辑(至少我对它的理解),我得到:
TypeError: can't pickle _csv.reader objects
我检查了这个错误(例如"Can't pickle <type '_csv.reader'>" error when using multiprocessing on Windows),但没有找到可以应用的解决方案。
【问题讨论】:
-
“似乎不起作用”究竟如何?您当前的代码似乎没有包含任何尝试。
-
由于我尝试了多种解决方案,我认为显示干净的代码更容易获得反馈。
-
这正是问题所在——准确地向我们展示您尝试了什么以及它是如何失败的。
-
我已经更新了我的描述。
-
如果您的输入不是 CSV,您不应该在刚刚成功转换的文本数据上使用
csv.reader()。尝试pickle可能表明存在更根本的误解。