python csv 导出数字字符串时格式错误,避免转换为科学计数

By 刘志军 , 2021-02-21, 分类: qa

csv

默认情况下导出的字符串全是数字时,在excel中打开时会被自动转换成科学计数格式

import csv

field_order = ["会诊单号"]
with open("导出.csv", 'w', encoding="utf8", newline="") as csvfile:
    writer = csv.DictWriter(csvfile, field_order)
    writer.writeheader()
    writer.writerow({"会诊单号": "20210217162737860175"})

微信截图_20210221095931.png

只需要在字符串后面加转义字符'\t'即可变成字符串

import csv

field_order = ["会诊单号"]
with open("导出.csv", 'w', encoding="utf8", newline="") as csvfile:
    writer = csv.DictWriter(csvfile, field_order)
    writer.writeheader()
    writer.writerow({"会诊单号": "20210217162737860175"+'\t'})

微信截图_20210221095426.png


关注公众号「Python之禅」,回复「1024」免费获取Python资源

python之禅