【问题标题】:.csv file is returning a "unicode error" when I attempt to import in python当我尝试在 python 中导入时,.csv 文件返回“unicode 错误”
【发布时间】:2019-05-08 05:45:31
【问题描述】:

我正在尝试在 Jupyter 笔记本上使用 pandas 导入 .csv 数据集文件。我不断收到相同的“unicode decode error”消息

我尝试过使用和不使用(r'file name'),并在反斜杠上加倍,以及使用正斜杠

这是我的代码

import pandas as pd
df = pd.read_csv(r'C:\users\justanotheregg\Downloads\medals.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 0x92 in position 12: invalid start byte

During handling of the above exception, another exception occurred:

【问题讨论】:

  • 你应该添加编码参数

标签: pandas csv python-import


【解决方案1】:

您的 CSV 文件显然不是 UTF-8 格式,但这是函数默认情况下所期望的。您需要找出文件的编码方式(如果是 Excel 导出,它可能是“cp-1252”;其他可能的编码,来自 ISO-8859 系列,没有 0x92 的映射,它在哪里结束单引号 在 1252 中,在 MS Office 中编写的文本中很常见)和 pass its name 作为 optional encoding parameter

【讨论】:

    【解决方案2】:

    我遇到了同样的问题。

    只需将其保存到谷歌表格,然后下载相同的文件,熊猫就可以在 jupyter notebook 中读取它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-09
      • 2022-01-04
      • 1970-01-01
      • 2021-03-18
      • 2018-01-02
      • 1970-01-01
      • 1970-01-01
      • 2021-07-24
      相关资源
      最近更新 更多