删除非法字符,以便数据框可以写入Excel
问题说明
我正在尝试使用ExcelWriter将数据框写入Excel电子表格,但它始终返回错误:
I am trying to write a dataframe to an Excel spreadsheet using ExcelWriter, but it keeps returning an error:
openpyxl.utils.exceptions.IllegalCharacterError
我猜想数据框中有些字符是ExcelWriter不喜欢的.似乎很奇怪,因为数据框是由三个Excel电子表格组成的,所以我看不到Excel可能不喜欢的字符!
I'm guessing there's some character in the dataframe that ExcelWriter doesn't like. It seems odd, because the dataframe is formed from three Excel spreadsheets, so I can't see how there could be a character that Excel doesn't like!
是否有任何方法可以遍历数据框并替换ExcelWriter不喜欢的字符?我什至不介意它是否只是删除它们.
Is there any way to iterate through a dataframe and replace characters that ExcelWriter doesn't like? I don't even mind if it simply deletes them.
从数据框中删除或替换非法字符的最佳方法是什么?
What's the best way or removing or replacing illegal characters from a dataframe?
正确答案
基于苏海鹏的回答,我添加了一个执行此操作的函数:
Based on Haipeng Su's answer, I added a function that does this:
dataframe = dataframe.applymap(lambda x: x.encode('unicode_escape').
decode('utf-8') if isinstance(x, str) else x)
基本上,如果存在unicode字符,它将转义.它奏效了,现在我可以再次写入Excel电子表格了!
Basically, it escapes the unicode characters if they exist. It worked and I can now write to Excel spreadsheets again!
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcgkefc
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
复制文件夹/文件而不修改属性?
it1352 07-15