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