繁体   English   中英

Python:对数据进行标记时出错。 C 错误:在源上调用 read(nbytes) 失败,输入 nzip 文件

[英]Python: Error tokenizing data. C error: Calling read(nbytes) on source failed with input nzip file

我正在使用conda python 2.7

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

我有阅读大型 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

您放置的路径可能实际上是folder的路径,而不是需要读取的file

Pandas.read_csv无法读取文件夹,需要明确兼容的文件名。

我并没有真正找到 python 解决方案,但是使用unix工具我设法找到了一个解决方案:

首先我使用zless myfile.txt.gz > uncompressedMyfile.txt然后我使用sed工具删除最后一行,因为我清楚地看到最后一行已损坏。

sed '$d' uncompressedMyfile.txt

我再次gzip -k 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 文件已损坏。 在将它传递给 pandas 之前,从尝试使用 zip 修复工具的来源获取此文件的正确副本。

尝试从 oneDrive 加载文件时也会出现此错误,并且您没有登录或它没有在您的电脑上运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM