欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

UnicodeDecodeError:‘utf-8‘codeccan‘tdecodebyte0xb4inposition6:invalidstartbyte(已解决)

时间:2023-08-18

自制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

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。