我正在创建一个新列和这个新文件并想要保存。但在 Excel 文件中,一列有一个字符。如何在保存过程中跳过此行或将行更改为正确的字符?
import pandas as pd
path = '/My Documents/Python/'
fileName = "test.xlsx"
# open the Excel file
ef = pd.ExcelFile(path+fileName)
# read the contents
df = pd.read_excel(path+fileName, sheet_name=ef.sheet_names[0])
print(df['Content'])
print(df['Engine'])
i = 1
for test in df['Content']:
try:
print(i)
print(test)
except:
print("An exception occurred")
break
i += 1
df['Test'] = 'value'
df.to_excel('My Documents/Python/Test_NEW.xlsx')
错误信息
data, consumed = self.encode(object, self.errors)
UnicodeEncodeError: 'utf-8' codec can't encode character '\ude7c' in position 470: surrogates not allowed
df['Content'] = df['Content'].astype(str)