Python数据帧非法字符错误进入';ascii';编解码错误
我正在尝试将pandas数据框写入excel。最初,我收到Python数据帧非法字符错误进入';ascii';编解码错误,python,dataframe,unicode,openpyxl,Python,Dataframe,Unicode,Openpyxl,我正在尝试将pandas数据框写入excel。最初,我收到 openpyxl.utils.exceptions.IllegalCharacterError 我决定: def export_file(clients): clients = clients.applymap(lambda x: x.encode('unicode_escape'). decode('utf-8') if isinstance(x, str) else x) client
openpyxl.utils.exceptions.IllegalCharacterError
我决定:
def export_file(clients):
clients = clients.applymap(lambda x: x.encode('unicode_escape').
decode('utf-8') if isinstance(x, str) else x)
clients.to_excel('all_clients.xlsx')
return()
结果是:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 17: ordinal not in range(128)
但是,如果我解决了unicode错误,就会得到初始的pyxl错误
我似乎无法解决一个错误而不得到相反的错误。有什么建议吗 用户xlsxwrite引擎,而不是直接使用pandas进行写入。这将解决错误
writer = pd.ExcelWriter('all_clients.xlsx', engine='xlsxwriter') # engine is set here
clients.to_excel(writer,'Sheet1') # it is called here to write the excel sheet
将解决openpyxl.utils.exceptions.IllegalCharacterError
在黑暗中拍摄(因为我们没有您的数据):尝试
latin-1
而不是utf-8
仍然得到ascii错误您可以在您的问题中添加任何示例数据吗?不幸的是,没有。我可以说,数据是由10个不同的excel文件和csv文件组成的。我们不需要所有的数据,只是一个复制问题的样本(可能是伪造的数据)。缩小范围。