python后面空格报错_python 使用xlsxwriter 写入数据时,当数据中链接的后面包含空格时(如:"http://*** "),导出问题打开报错_Python_七九推...

python 在使用xlsxwriter组件写入数据时,当数据包含类似“http://*** /”数据时,导出的excel,打开时会提示如下错误:

67dc7c9a6b7f7ae4464a8b5e669d2957.png

没有查到相关的资料处理这个问题,可能原因为excel识别为链接,与内置库冲突导致,对数据准确性无大影响的情况下,只能临时通过字符替换解决:

if keyword.startswith('http://') and keyword.find(' ') >= 0:

keyword = keyword.replace('','')

截取的部分代码如下:

1 os.chdir('/data/scripts/file/')

2 filename = "统计-" + last_month + ".xlsx"

3

4 # 将查询的结果写入至excel

5 workbook = xlsxwriter.workbook(filename)

6 worksheet = workbook.add_worksheet(name="数据统计")

7 worksheet.set_column("b:b", 40)

8 #header_style = workbook.add_format({})

9 header_style = workbook.add_format({"bold" : true, "font_size" : 10, "align" : "center", "bg_color" : "#dcdcdc", "top" : 1, "bottom" : 1, "left" : 1, "right" : 1})

10 #item_style = workbook.add_format({})

11 item_style = workbook.add_format({"font_size" : 10, "align" : "center", "top" : 1, "bottom" : 1, "left" : 1, "right" : 1})

12

13 # 写入表头

14 worksheet.write(0,0,"序号",header_style)

15 worksheet.write(0,1,"词",header_style)

16 worksheet.write(0,2,"总数",header_style)

17 sorted_search_dict = sorted(search_dict.items(), key=lambda d:d[1], reverse = true)

18 # 写入数据项

19 data_row = 0

20 for i in sorted_search_dict:

21 data_row += 1

22 keyword = i[0].strip()

23 if keyword.startswith('http://'):

24 keyword = keyword.replace('','')

25 #print(str(data_row) + "," + i[0] + "," + str(i[1]))

26 worksheet.write(data_row , 0, data_row, item_style)

27 worksheet.write(data_row , 1, keyword, item_style)

28 worksheet.write(data_row , 2, i[1], item_style)

29 workbook.close()

希望与广大网友互动??

点此进行留言吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值