在Windows上使用pandas读取CSV文件时出现错误

6

我正在尝试用 pandas.csv_read 读取一个大小为57MB的文件。该文件包含一个标题(5行),之后是整数值,最后是浮点值:

info         
       2681087         53329       1287215       1287215         53328
RSA                    53328         53328       1287215             0
(I14)           (I14)           (d25.15)            (d25.15)            
F                          1         5332   
           1
          33
          61
          92
         128
         ...
         165
         205
         239
         272
    0.112474585277959E+09
    0.126110931411177E+09
    0.515995872032845E+09
    0.126110931411175E+09
   -0.194634413074014E+09
    0.112474585277950E+09
    ...

当我读取文本文件时:
import pandas as pd
pd.read_csv(file, skiprows=5+n_int_values, header=None, engine='c', 
            dtype=np.float, low_memory=False)

结果出现错误:
---------------------------------------------------------------------------
CParserError                              Traceback (most recent call last)
<ipython-input-118-699921ac7a12> in <module>()
----> 1 a=pd.read_csv(loc, skiprows=5+n_coloums+n_rows, header=None, engine='c', low_memory=False, error_bad_lines=False)

C:\Anaconda\lib\site-packages\pandas\io\parsers.pyc in parser_f(filepath_or_buffer, sep, dialect, compression, doublequote, escapechar, quotechar, quoting, skipinitialspace, lineterminator, header, index_col, names, prefix, skiprows, skipfooter, skip_footer, na_values, na_fvalues, true_values, false_values, delimiter, converters, dtype, usecols, engine, delim_whitespace, as_recarray, na_filter, compact_ints, use_unsigned, low_memory, buffer_lines, warn_bad_lines, error_bad_lines, keep_default_na, thousands, comment, decimal, parse_dates, keep_date_col, dayfirst, date_parser, memory_map, float_precision, nrows, iterator, chunksize, verbose, encoding, squeeze, mangle_dupe_cols, tupleize_cols, infer_datetime_format, skip_blank_lines)
    468                     skip_blank_lines=skip_blank_lines)
    469 
--> 470         return _read(filepath_or_buffer, kwds)
    471 
    472     parser_f.__name__ = name

C:\Anaconda\lib\site-packages\pandas\io\parsers.pyc in _read(filepath_or_buffer, kwds)
    254         return parser
    255 
--> 256     return parser.read()
    257 
    258 _parser_defaults = {

C:\Anaconda\lib\site-packages\pandas\io\parsers.pyc in read(self, nrows)
    713                 raise ValueError('skip_footer not supported for iteration')
    714 
--> 715         ret = self._engine.read(nrows)
    716 
    717         if self.options.get('as_recarray'):

C:\Anaconda\lib\site-packages\pandas\io\parsers.pyc in read(self, nrows)
   1162 
   1163         try:
-> 1164             data = self._reader.read(nrows)
   1165         except StopIteration:
   1166             if nrows is None:

pandas\parser.pyx in pandas.parser.TextReader.read (pandas\parser.c:7426)()

pandas\parser.pyx in pandas.parser.TextReader._read_rows (pandas\parser.c:8377)()

pandas\parser.pyx in pandas.parser.raise_parser_error (pandas\parser.c:20728)()

CParserError: Error tokenizing data. C error: Buffer overflow caught - possible malformed input file.

有什么想法为什么会出现这种情况,以及如何修复它。
注意:如果使用engine='python',则txt文件会正常加载。
注意2:error_bad_lines=False不会改变任何内容。
更新:这在pandas 0.16.0上发生。在旧版本0.14.1中,它可以正常工作。
更新2:该错误已在pandas 0.16.1中修复。

1
在Windows和Ubuntu上使用相同版本的Pandas吗? - Bob Haffner
Bob,感谢您的评论。我在Ubuntu上安装了pandas 0.14.1。当我更新它时,出现了同样的问题。 - blaz
1
我将问题移动到:链接 - blaz
在我的电脑上运行良好,使用的是Windows 7 64位操作系统、Python 3.4 和Pandas 0.16.0。 - EdChum
无法工作,Win 7 x64,Py2.7 x86,Pandas(0.17.1)。 - Mithril
显示剩余2条评论
1个回答

0

这个 bug 在 pandas 0.16.1 中已经修复。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接