python 在使用xlsxwriter组件写入数据时,当数据包含类似“http://*** /”数据时,导出的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()
希望与广大网友互动??
点此进行留言吧!