我在用 conda python 2.7
python --version
Python 2.7.12 :: Anaconda 2.4.1 (x86_64)
Run Code Online (Sandbox Code Playgroud)
我有休闲的方法来读取大的gzip文件:
df = pd.read_csv(os.path.join(filePath, fileName),
sep='|', compression = 'gzip', dtype='unicode', error_bad_lines=False)
Run Code Online (Sandbox Code Playgroud)
但是当我读取文件时,出现以下错误:
pandas.parser.CParserError: Error tokenizing data. C error: Calling read(nbytes) on source failed. Try engine='python'.
Segmentation fault: 11
Run Code Online (Sandbox Code Playgroud)
我阅读了所有现有的答案,但是其中大多数问题都有错误,例如其他列。我已经用error_bad_lines=False选项处理了。
我在这里有什么选择?
当我尝试解压缩文件时发现了一些有趣的东西:
gunzip -k myfile.txt.gz
gunzip: myfile.txt.gz: unexpected end of file
gunzip: myfile.txt.gz: uncompress failed
Run Code Online (Sandbox Code Playgroud)
我并没有真正找到 python 解决方案,而是使用unix我设法找到解决方案的工具:
首先我使用zless myfile.txt.gz > uncompressedMyfile.txt
然后我使用sed工具删除最后一行,因为我清楚地看到最后一行已损坏。
sed '$d' uncompressedMyfile.txt
我再次压缩文件 gzip -k uncompressedMyfile.txt
我能够使用以下python代码成功读取文件:
try:
df = pd.read_csv(os.path.join(filePath, fileName),
sep='|', compression = 'gzip', dtype='unicode', error_bad_lines=False)
except CParserError:
print "Something wrong the file"
return df
Run Code Online (Sandbox Code Playgroud)