使用 Pandas 时如何修复编解码器编码错误

How do I fix a codec encode error while using Pandas

我正在读取我的 CSV 并进行一些数据清理,但我收到以下错误...

UnicodeEncodeError: 'charmap' codec can't encode character '\x96' in position 6193: character maps to undefined

如果我注释掉 sort_valuesdrop_duplicates 函数,则不会产生错误。我该如何解决这个错误?我是初学者,今天花了几个小时试图 google 一个答案,但我一无所获。下面的代码...

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)

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