【问题标题】:'utf-8' codec can't decode byte 0xa3 in position 28: invalid start byte“utf-8”编解码器无法解码位置 28 中的字节 0xa3:无效的起始字节
【发布时间】:2021-09-23 01:18:30
【问题描述】:

我正在尝试使用 pandas 库从谷歌驱动器读取 CSV 文件。
但是,我遇到了一个问题“UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa3 in position 28: invalid start byte”

我的代码

df = pd.read_csv("/content/gdrive/My Drive/data/OnlineRetail.csv")

输出

---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._convert_tokens()

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._convert_with_dtype()

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._string_convert()

pandas/_libs/parsers.pyx in pandas._libs.parsers._string_box_utf8()

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa3 in position 28: invalid start byte

During handling of the above exception, another exception occurred:

UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-6-65a06557fa8d> in <module>()
----> 1 df = pd.read_csv("/content/gdrive/My Drive/data/OnlineRetail.csv")

3 frames
/usr/local/lib/python3.7/dist-packages/pandas/io/parsers.py in read_csv(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, dialect, error_bad_lines, warn_bad_lines, delim_whitespace, low_memory, memory_map, float_precision)
    686     )
    687 
--> 688     return _read(filepath_or_buffer, kwds)
    689 
    690 

/usr/local/lib/python3.7/dist-packages/pandas/io/parsers.py in _read(filepath_or_buffer, kwds)
    458 
    459     try:
--> 460         data = parser.read(nrows)
    461     finally:
    462         parser.close()

/usr/local/lib/python3.7/dist-packages/pandas/io/parsers.py in read(self, nrows)
   1196     def read(self, nrows=None):
   1197         nrows = _validate_integer("nrows", nrows)
-> 1198         ret = self._engine.read(nrows)
   1199 
   1200         # May alter columns / col_dict

/usr/local/lib/python3.7/dist-packages/pandas/io/parsers.py in read(self, nrows)
   2155     def read(self, nrows=None):
   2156         try:
-> 2157             data = self._reader.read(nrows)
   2158         except StopIteration:
   2159             if self._first_chunk:

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader.read()

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._read_low_memory()

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._read_rows()

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._convert_column_data()

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._convert_tokens()

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._convert_with_dtype()

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._string_convert()

pandas/_libs/parsers.pyx in pandas._libs.parsers._string_box_utf8()

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa3 in position 28: invalid start byte

【问题讨论】:

  • 试试 - pd.read_csv('...',encoding='utf-8')
  • 在我尝试了您的建议后“UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa3 in position 79780: invalid start byte”
  • 或者试试encoding=ascii。查看here 了解不同类型的编码
  • 或者你可以试试read_csv(filename, encoding= 'unicode_escape')
  • 显而易见的答案是它没有以 UTF-8 编码。字节A3 代表 ISO-8859-1(又名 Latin-1)和 cp1252(又名 Windows-1252)中的英镑符号 (£),因此如果这是文件位于第 28 位。请注意,ISO-8859-1 可以解码任何内容,因此您仍然需要查看结果以确保没有错误。如果您提供 CSV 前几行的 hexdump,我们可以重现该问题。

标签: python pandas csv unicode utf-8


【解决方案1】:

有同样的问题。它可能不是 utf-8 编码。试着弄清楚它是什么。您可以通过在记事本++中打开它来做到这一点。顶部有编码菜单,看看选择了什么。

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 2018-02-10
  • 2018-06-12
  • 2020-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多