Python数据帧非法字符错误变成'ascii'编解码器解码错误

问题描述 投票:0回答:1

我正在尝试将一个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)

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

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

python dataframe unicode openpyxl
1个回答
1
投票

用户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。

© www.soinside.com 2019 - 2024. All rights reserved.