【发布时间】:2020-04-15 06:46:48
【问题描述】:
我正在阅读我的 CSV 文件并进行一些数据清理,但出现以下错误...
UnicodeEncodeError:“charmap”编解码器无法在位置 6193 编码字符“\x96”:字符映射到未定义
如果我注释掉 sort_values 或 drop_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