CSV文件的读取与写入

写入CSV文件

1
2
3
4
5
save_path = '/home/wp/st_detection/download_code/toy.csv'
with open(save_path, 'w', newline='') as csvfile:
    det_writer = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
    det_writer.writerow(['brush_hair/April_09_brush_hair_u_nm_np1_ba_goo_0', 0.1, 0.1, 0.5, 0.5, 0.98])
    det_writer.writerow(['brush_hair/April_09_brush_hair_u_nm_np1_ba_goo_0', 0.2, 0.2, 0.6, 0.6, 0.92])

写入结果:

 

读取CSV文件

1
2
3
4
5
path = '/home/wp/st_detection/download_code/toy.csv'
with open(path, newline='') as csvfile:
    det_reader = csv.reader(csvfile, delimiter=',', quotechar='|')
    for row in det_reader:  # row 是一个列表
        print('   '.join(row))

输出结果:

 

本文参考:csv --- CSV 文件读写 — Python 3.12.0 文档

 

posted @   Picassooo  阅读(29)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
历史上的今天:
2022-10-19 转:pytorch张量维度操作(拼接、维度扩展、压缩、转置、重复等)
点击右上角即可分享
微信分享提示