Python错误:令牌化数据时出错。C错误:对源进行read(nbytes)调用失败,输入为nzip fi。

2024-04-24 02:54:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用condapython2.7

python --version
Python 2.7.12 :: Anaconda 2.4.1 (x86_64)

我有fallowing方法来读取大型gzip文件:

df = pd.read_csv(os.path.join(filePath, fileName),
     sep='|', compression = 'gzip', dtype='unicode', error_bad_lines=False)

但当我读取文件时,会出现以下错误:

pandas.parser.CParserError: Error tokenizing data. C error: Calling read(nbytes) on source failed. Try engine='python'.
Segmentation fault: 11

我阅读了所有现有的答案,但大多数问题都有错误,如附加的专栏。我已经用error_bad_lines=False选项来处理了。

我在这里有什么选择?

当我试图解压缩文件时发现了一些有趣的东西:

gunzip -k myfile.txt.gz 
gunzip: myfile.txt.gz: unexpected end of file
gunzip: myfile.txt.gz: uncompress failed

Tags: 文件txtfalsereadversion错误erroranaconda
2条回答

我并没有真正找到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

输入zip文件已损坏。从try to use zip repairing tools的源获取此文件的正确副本,然后再将其传递给pandas。

相关问题 更多 >