hengdin

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
统计
 
openpyxl.utils.exceptions.IllegalCharacterError

  • 原因
    Excel表中有非法字符,这些字符都是八进制的,需要进行清洗

  • 解决办法一:(自己亲测有效)

import re
def data_clean(text):
    # 清洗excel中的非法字符,都是不常见的不可显示字符,例如退格,响铃等
    ILLEGAL_CHARACTERS_RE = re.compile(r'[\000-\010]|[\013-\014]|[\016-\037]')
    text = ILLEGAL_CHARACTERS_RE.sub(r'', text)
    return text

df = df.fillna('').astype(str)
for col in df.columns:
    df[col] = df[col].apply(lambda x: data_clean(x))
df.to_excel(base_path + 'result.xlsx', index=False)

  • 解决办法二(该方法未测试,网站找到的):
import xlsxwriter
df.to_excel(base_path + 'result.xlsx', engine='xlsxwriter', index=False, encoding='utf-8')

posted on   hengdin  阅读(4170)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
 
点击右上角即可分享
微信分享提示