pythonで発生した問題を学習します.読み取りテーブルの境界文字が間違っています

9291 ワード

これはO'Rellyの冒頭の練習で、利用してみました.http://grouplens.org/datasets/movielens/上の映画評論データに関するdatasetの分析.圧縮パッケージのuserデータの読み取りを開始するとき、次のコードを使用しました.
 users = pd.read_table('C:\Python27\data\ml\u.user',
     sep = "::",
     header = None,
     names = unames)

(実は本のコードです)しかし、エラーメッセージを提示し、エラーメッセージは以下の通りです.
C:\Python27\Scripts\ipython:1: 
ParserWarning: 
    Falling back to the 'python' engine because the 'c' engine does not support regex separators; 
    you can avoid this warning by specifying engine='python'.

ヒントを見るとエンジンのせいか、私は元のpd.read_tableパラメータリストにengine='python'を加えたのか、それともエラーを表示したのか、エラーの内容は以下の通りです.
ValueError     Traceback (most recent call last)
 in ()
----> 1 users = pd.read_table('C:\Python27\data\ml\u.user',sep = "::",header = None,names = unames,engine='python')

C:\Python27\lib\site-packages\pandas\io\parsers.pyc in parser_f(filepath_or_buffer, sep, dialect, compression, doubl
te, escapechar, quotechar, quoting, skipinitialspace, lineterminator, header, index_col, names, prefix, skiprows, sk
oter, skip_footer, na_values, true_values, false_values, delimiter, converters, dtype, usecols, engine, delim_whites
, as_recarray, na_filter, compact_ints, use_unsigned, low_memory, buffer_lines, warn_bad_lines, error_bad_lines, kee
fault_na, thousands, comment, decimal, parse_dates, keep_date_col, dayfirst, date_parser, memory_map, float_precisio
rows, iterator, chunksize, verbose, encoding, squeeze, mangle_dupe_cols, tupleize_cols, infer_datetime_format, skip_
k_lines)
    496                     skip_blank_lines=skip_blank_lines)
    497
--> 498         return _read(filepath_or_buffer, kwds)
    499
    500     parser_f.__name__ = name

C:\Python27\lib\site-packages\pandas\io\parsers.pyc in _read(filepath_or_buffer, kwds)
    283         return parser
    284
--> 285     return parser.read()
    286
    287 _parser_defaults = {

C:\Python27\lib\site-packages\pandas\io\parsers.pyc in read(self, nrows)
    745                 raise ValueError('skip_footer not supported for iteration')
    746
--> 747         ret = self._engine.read(nrows)
    748
    749         if self.options.get('as_recarray'):

C:\Python27\lib\site-packages\pandas\io\parsers.pyc in read(self, rows)
   1603             content = content[1:]
   1604
-> 1605         alldata = self._rows_to_cols(content)
   1606         data = self._exclude_implicit_index(alldata)
   1607

C:\Python27\lib\site-packages\pandas\io\parsers.pyc in _rows_to_cols(self, content)
   1978             msg = ('Expected %d fields in line %d, saw %d' %
   1979                    (col_len, row_num + 1, zip_len))
-> 1980             raise ValueError(msg)
   1981
   1982         if self.usecols:

ValueError: Expected 5 fields in line 1, saw 1

In [9]: users = pd.read_table('C:\\Python27\\data\\ml\\u.user',sep = "::",header = None,names = unames,engine='pytho
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
 in ()
----> 1 users = pd.read_table('C:\\Python27\\data\\ml\\u.user',sep = "::",header = None,names = unames,engine='pytho

C:\Python27\lib\site-packages\pandas\io\parsers.pyc in parser_f(filepath_or_buffer, sep, dialect, compression, doubl
te, escapechar, quotechar, quoting, skipinitialspace, lineterminator, header, index_col, names, prefix, skiprows, sk
oter, skip_footer, na_values, true_values, false_values, delimiter, converters, dtype, usecols, engine, delim_whites
, as_recarray, na_filter, compact_ints, use_unsigned, low_memory, buffer_lines, warn_bad_lines, error_bad_lines, kee
fault_na, thousands, comment, decimal, parse_dates, keep_date_col, dayfirst, date_parser, memory_map, float_precisio
rows, iterator, chunksize, verbose, encoding, squeeze, mangle_dupe_cols, tupleize_cols, infer_datetime_format, skip_
k_lines)
    496                     skip_blank_lines=skip_blank_lines)
    497
--> 498         return _read(filepath_or_buffer, kwds)
    499
    500     parser_f.__name__ = name

C:\Python27\lib\site-packages\pandas\io\parsers.pyc in _read(filepath_or_buffer, kwds)
    283         return parser
    284
--> 285     return parser.read()
    286
    287 _parser_defaults = {

C:\Python27\lib\site-packages\pandas\io\parsers.pyc in read(self, nrows)
    745                 raise ValueError('skip_footer not supported for iteration')
    746
--> 747         ret = self._engine.read(nrows)
    748
    749         if self.options.get('as_recarray'):

C:\Python27\lib\site-packages\pandas\io\parsers.pyc in read(self, rows)
   1603             content = content[1:]
   1604
-> 1605         alldata = self._rows_to_cols(content)
   1606         data = self._exclude_implicit_index(alldata)
   1607

C:\Python27\lib\site-packages\pandas\io\parsers.pyc in _rows_to_cols(self, content)
   1978             msg = ('Expected %d fields in line %d, saw %d' %
   1979                    (col_len, row_num + 1, zip_len))
-> 1980             raise ValueError(msg)
   1981
   1982         if self.usecols:

ValueError: Expected 5 fields in line 1, saw 1

上stack overflowで検索した解決策は,経路中の単斜棒''を二重斜棒''に変換し,依然としてエラーを報告し,エラー内容を同上に保つことである.
さらに探してみると、学习python出现的问题_读取表格分界符不对_第1张图片が元のpd.read_tableパラメータリストのsep = "::"sep = Noneに変更した後、問題が解決したという返事がありました.