python openpyxl读写excel相关

1. openpyxl 报错 openpyxl.utils.exceptions.IllegalCharacterError, 使用ILLEGAL_CHARACTERS_RE 过滤非法字符

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from openpyxl import Workbook
from openpyxl.cell.cell import ILLEGAL_CHARACTERS_RE

wb = Workbook()
ws = wb.active
ws.title = 'data'
cl = ['学号', '姓名', '性别', '年龄', '电话号码', '生日']   #表头
ws.append(cl)
big_lists = [['1', '张三', '男', '14', '18254256332', '1999-06-05'],
	['2', '李四', '女', '13', '17025463298', '1997-06-06'],
	['3', '王二麻', '男', '14', '133659875623', '1998-03-08'],
	['4', '刘六', '女', '15', '19832652354', '1999-09-09']]     #可迭代的数据

for alist in big_lists:
    tmp_alist =  [ILLEGAL_CHARACTERS_RE.sub(r'',ct) for ct in alist]  #去掉字符串中会导致报错的字符
    #print(tmp_alist)
    ws.append(tmp_alist)
wb.save('test.xlsx')
上一篇
下一篇