python csv模块使用教程

CSV (Comma Separated Values) 格式是电子表格和数据库中最常见的输入、输出文件格式。在 RFC 4180 规范推出的很多年前,CSV 格式就已经被开始使用了,由于当时并没有合理的标准,不同应用程序读写的数据会存在细微的差别。这种差别让处理多个来源的 CSV 文件变得困难。但尽管分隔符会变化,此类文件的大致格式是相似的,所以编写一个单独的模块以高效处理此类数据,将程序员从读写数据的繁琐细节中解放出来是有可能的。

csv 模块实现了 CSV 格式表单数据的读写。其提供了诸如“以兼容 Excel 的方式输出数据文件”或“读取 Excel 程序输出的数据文件”的功能,程序员无需知道 Excel 所采用 CSV 格式的细节。此模块同样可以用于定义其他应用程序可用的 CSV 格式或定义特定需求的 CSV 格式。

推荐教程: python官网:csv模块中文使用教程

官网的教程是最全面的,但是比较晦涩,大家要耐心阅读!

总览

csv总共有二重要API:

  • read
    • 函数接口:

      with open('eggs.csv', newline='') as csvfile:
          spamreader = csv.reader(csvfile)
          for row in spamreader:
              print(row)
      
    • 字典接口在这里插入图片描述

      with open("./test.csv", "r") as csvfile:
          reader = csv.DictReader(csvfile)
          for row in reader:
              print(row['nnc'])
              print(row['input'])
              print(row['output'])
      
  • write
    • 函数接口: 在这里插入图片描述

      test_list = [
          "January",
          "February",
          "March",
          "April",
          "May",
          "June",
      ]
      with open("./test2.csv", "w") as f:
          cw = csv.writer(f)
          cw.writerow(test_list)
      
    • 字典接口在这里插入图片描述

      	with open("test.csv", "w") as f:
      	    fieldnames = ['nnc', 'input', 'output']
      	    writer = csv.DictWriter(f, fieldnames=fieldnames)
      	    writer.writeheader()
      	    for j in j_info:
      	        writer.writerow(j)
      	        print(j)
      

write csv file

  • 字典接口

写csv

假如说要写入以下内容,则可以从json文件中,序列化对象,然后转位json对象,将json对象在写入csv文件中。
在这里插入图片描述


import csv
import json

with open("./test.json", "r") as f:
    j_info = json.loads(f.read())
with open("test.csv", "w") as f:
    fieldnames = ['nnc', 'input', 'output']
    writer = csv.DictWriter(f, fieldnames=fieldnames)
    writer.writeheader()
    for j in j_info:
        writer.writerow(j)
        print(j)


  • test.json 文件内容
[
    {
        "nnc": "name",
        "input": [
            1,
            2,
            3
        ],
        "output": [
            2,
            3,
            4
        ]
    },
    {
        "nnc": "name",
        "input": [
            1,
            2,
            3
        ],
        "output": [
            2,
            3,
            4
        ]
    },
    {
        "nnc": "name",
        "input": [
            1,
            2,
            3
        ],
        "output": [
            2,
            3,
            4
        ]
    },
    {
        "nnc": "name",
        "input": [
            1,
            2,
            3
        ],
        "output": [
            2,
            3,
            4
        ]
    },
    {
        "nnc": "name",
        "input": [
            1,
            2,
            3
        ],
        "output": [
            2,
            3,
            4
        ]
    }
]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

早睡的叶子

你的鼓励就是我的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值