将MSR释义语料库读入熊猫

时间:2017-05-29 02:42:28

标签: python pandas corpus tab-delimited

我从MSR下载了MSR释义语料库,并尝试将其加载到数据帧中,但出现以下错误:

import pandas as pd
df = pd.read_csv(r'C:\MSRParaphraseCorpus\msr_paraphrase_test.txt', sep = '\t' )

错误:

CParserError                              Traceback (most recent call last)
<ipython-input-10-35d992467320> in <module>()
----> 1 df = pd.read_csv(r'C:\MSRParaphraseCorpus\msr_paraphrase_test.txt', sep = '\t' )

c:\python34\lib\site-packages\pandas\io\parsers.py 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)
    472                     skip_blank_lines=skip_blank_lines)
    473 
--> 474         return _read(filepath_or_buffer, kwds)
    475 
    476     parser_f.__name__ = name

c:\python34\lib\site-packages\pandas\io\parsers.py in _read(filepath_or_buffer, kwds)
    258         return parser
    259 
--> 260     return parser.read()
    261 
    262 _parser_defaults = {

c:\python34\lib\site-packages\pandas\io\parsers.py in read(self, nrows)
    719                 raise ValueError('skip_footer not supported for iteration')
    720 
--> 721         ret = self._engine.read(nrows)
    722 
    723         if self.options.get('as_recarray'):

c:\python34\lib\site-packages\pandas\io\parsers.py in read(self, nrows)
   1168 
   1169         try:
-> 1170             data = self._reader.read(nrows)
   1171         except StopIteration:
   1172             if nrows is None:

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

pandas\parser.pyx in pandas.parser.TextReader._read_low_memory (pandas\parser.c:7806)()

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

pandas\parser.pyx in pandas.parser.TextReader._tokenize_rows (pandas\parser.c:8297)()

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

CParserError: Error tokenizing data. C error: Expected 5 fields in line 34, saw 6

所以我看了第34行,它看起来非常好。

  fname = r'C:\MSRParaphraseCorpus\msr_paraphrase_test.txt'
    with open(fname, encoding="utf8") as f:
        content = f.readlines()

    content[34]

输出:

'0\t1268500\t1268733\tAgainst the Japanese currency, the euro was at 135.92/6.04 yen against the late New York level of 136.03/14.\tThe dollar was at 117.85 yen against the Japanese currency, up 0.1 percent.\n'

1 个答案:

答案 0 :(得分:1)

问题在于第34行的开放引号(如我的评论中所述)。通过传入csv.QUOTE_NONE禁用csv阅读器的引用。尝试:

import csv
import pandas as pd

df = pd.read_csv(r'C:\MSRParaphraseCorpus\msr_paraphrase_test.txt', sep = '\t', quoting=csv.QUOTE_NONE)