我有一个 25GB 的文本文件。所以我将它压缩为 tar.gz,它变成了 450 MB。现在我想从 python 中读取该文件并处理文本数据。为此我提到了 question .但在我的情况下,代码不起作用。代码如下:
import tarfile
import numpy as np
tar = tarfile.open("filename.tar.gz", "r:gz")
for member in tar.getmembers():
f=tar.extractfile(member)
content = f.read()
Data = np.loadtxt(content)
错误如下:
Traceback (most recent call last):
File "dataExtPlot.py", line 21, in <module>
content = f.read()
AttributeError: 'NoneType' object has no attribute 'read'
还有,还有其他方法可以完成这项任务吗?
最佳答案
docs告诉我们,如果成员不是常规文件或链接,extractfile() 将返回 None。
一种可能的解决方案是跳过 None 结果:
tar = tarfile.open("filename.tar.gz", "r:gz")
for member in tar.getmembers():
f = tar.extractfile(member)
if f is not None:
content = f.read()
关于python - 在 Python 中读取 .tar.gz 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37474767/