美文网首页
关于读取写入csv文件

关于读取写入csv文件

作者: 葡萄柚子茶 | 来源:发表于2020-03-25 10:34 被阅读0次

    with open(current_date+'.csv', encoding='utf-8') as f:
        f_csv = csv.DictReader(f)
        for row in f_csv:
            print(row)

    current_date = time.strftime('%Y-%m-%d', time.localtime(time.time()))
    current_time = time.strftime('%H:%M:%S', time.localtime(time.time()))
    f = open(current_date+'.csv', 'a', encoding='utf-8', newline='')
    csv_writer = csv.writer(f)
    csv_writer.writerow([current_time, str(bo_total) + '\t', str(so_total) + '\t'])
    f.close()

避免生成数据之间有空行

f = open(current_date+'.csv', 'a', encoding='utf-8', newline='')

避免生成数据记录成科学计数法
在代码端将这个字段分离出来,改变成字符串,并加上" \t" 就可以解决这个问题

csv_writer.writerow([current_time, str(bo_total) + '\t', str(so_total) + '\t'])

相关文章

网友评论

      本文标题:关于读取写入csv文件

      本文链接:https://www.haomeiwen.com/subject/ubktuhtx.html