【问题标题】:How do I fix a codec encode error while using Pandas如何在使用 Pandas 时修复编解码器编码错误
【发布时间】:2020-04-15 06:46:48
【问题描述】:

我正在阅读我的 CSV 文件并进行一些数据清理,但出现以下错误...

UnicodeEncodeError:“charmap”编解码器无法在位置 6193 编码字符“\x96”:字符映射到未定义

如果我注释掉 sort_valuesdrop_duplicates 函数,则不会产生错误。我该如何解决这个错误?我是一个初学者,今天花了几个小时试图用谷歌搜索答案,但我什么也没有。下面的代码...

import pandas as pd

theData = pd.read_csv(r'my.csv', encoding='latin1')

theData = theData.drop_duplicates(subset=['BROADCAST','ARTIST','SONG','LABEL','ALBUM'])
epNums = theData['BROADCAST'].str[15:]
epNums = epNums.str[:3]
theData['ep num'] = epNums
theData['ep num'] = theData['ep num'].astype(int)
theData = theData.sort_values(by=['ep num'])

print(theData)

【问题讨论】:

  • 您使用encoding='latin1' 有什么具体原因吗?您是否考虑过将编码转换为utf-8
  • 我试过 UTF-8、ascii 还是没有成功。

标签: python pandas unicode encode codec


【解决方案1】:

这个错误似乎只发生在我的电脑上,因为它在我朋友的 Mac 上运行良好。但解决问题的方法是将encoding= 替换为engine='python

【讨论】:

    猜你喜欢
    • 2018-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-09
    • 1970-01-01
    • 2022-07-26
    • 1970-01-01
    • 2014-02-03
    相关资源
    最近更新 更多