自制csv文件,开头含中文。
在调用pandas.read_csv()时:
df = pandas.read_csv(csv)
打印df,遇到报错:
File "E:Python39libsite-packagespandasutil_decorators.py", line 311, in wrapper
return func(*args, **kwargs)
File "E:Python39libsite-packagespandasioparsersreaders.py", line 680, in read_csv
return _read(filepath_or_buffer, kwds)
File "E:Python39libsite-packagespandasioparsersreaders.py", line 575, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)
File "E:Python39libsite-packagespandasioparsersreaders.py", line 933, in __init__
self._engine = self._make_engine(f, self.engine)
File "E:Python39libsite-packagespandasioparsersreaders.py", line 1231, in _make_engine
return mapping[engine](f, **self.options)
File "E:Python39libsite-packagespandasioparsersc_parser_wrapper.py", line 75, in __init__
self._reader = parsers.TextReader(src, **kwds)
File "pandas_libsparsers.pyx", line 544, in pandas._libs.parsers.TextReader.__cinit__
File "pandas_libsparsers.pyx", line 633, in pandas._libs.parsers.TextReader._get_header
File "pandas_libsparsers.pyx", line 847, in pandas._libs.parsers.TextReader._tokenize_rows
File "pandas_libsparsers.pyx", line 1952, in pandas._libs.parsers.raise_parser_error
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb4 in position 6: invalid start byte
按照有人的建议,改为:
df = pandas.read_csv(csv, encoding='ISO-8859-1')
或者
df = pandas.read_csv(csv, encoding='latin1')
可以打印了,但是乱码。
中文的事情还得中文解决,最后改为:
df = pandas.read_csv(csv, encoding='gb18030')
没问题了。
2022-02-02
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)