Python数据帧非法字符错误转换为"ASCII"编解码器解码错误 [英] Python dataframe illegal character error into 'ascii' codec decode error

查看:112
本文介绍了Python数据帧非法字符错误转换为"ASCII"编解码器解码错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个熊猫数据框来表现出色.最初,我收到了

I am attempting to write a pandas dataframe to excel. Initially, I received

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错误.

However, if i resolve the unicode error I get the initial pyxl error.

我似乎无法解决一个错误而没有得到相反的错误.有什么建议?

I seem to be unable to resolve one error without getting the opposing error. Any suggestions?

推荐答案

用户xlsxwrite引擎而不是直接使用熊猫编写.这样可以解决错误.

Instead of writing directly using pandas, user xlsxwrite engine. This will resolve the errors.

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将得到解决.

openpyxl.utils.exceptions.IllegalCharacterError will be resolved.

这篇关于Python数据帧非法字符错误转换为"ASCII"编解码器解码错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆