python - 'utf- 8' codec can' t 解码字节 0xa3 在位置 28 : invalid start byte

标签 python pandas csv unicode utf-8

我正在尝试使用 Pandas 库从谷歌驱动器读取 CSV 文件。
但是,我遇到了一个问题 “UnicodeDecodeError:‘utf-8’编解码器无法解码位置 28 中的字节 0xa3:起始字节无效”
我的代码

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

最佳答案

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

关于python - 'utf- 8' codec can' t 解码字节 0xa3 在位置 28 : invalid start byte,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68371667/

相关文章:

java - Spring MVC 项目 - 读取 CSV 文件 - HTTP 状态 500 - data.csv(访问被拒绝)

r - 使用 R 中 readr 中的 read_csv 将文本作为指定列以 [type] 打开

python - 做多个数据库计数还是用Django在代码中计数效率更高?

python - Python 2.6 中的 Maketrans

pandas - 两个日期列之间的年份 = 'Timedelta' 对象没有属性 'item'

python - 更改 pandas 日期格式以删除前导零的更好方法?

vba - 如何在 VBA 中更改文件扩展名

python - 按列号 Pandas 选择非相邻列

python - 使用 gspread 获取所有谷歌表格的列表?

python - 从数据框中的列中选择特定值