当前位置: 移动技术网 > IT编程>脚本编程>Python > python 使用xlsxwriter 写入数据时,当数据中链接的后面包含空格时(如:"http://*** "),导出问题打开报错

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

2019年07月02日  | 移动技术网IT编程  | 我要评论

偷窥孔,奇皇后37,口袋妖怪黑梦娜

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

没有查到相关的资料处理这个问题,可能原因为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()

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网