• Python标准库--csv模块


    csv模块

    读写逗号分隔值的文件

    一、准备文件

    csv_test.csv

    user,passwd,date
    a1,a2,2018-04-23
    b1,b2,2018-04-24
    c1,c2,2018-04-25
    

    二、读文件

    • reader()
        with open("csv_test.csv", "rt") as f:
            reader = csv.reader(f)
    
            for row in reader:
                print(row)
    
    ['user', 'passwd', 'date']
    ['a1', 'a2', '2018-04-23']
    ['b1', 'b2', '2018-04-24']
    ['c1', 'c2', '2018-04-25']
    
    • DictReader()
        with open("csv_test.csv", "rt") as f:
            dict_reader = csv.DictReader(f)
    
            for row in dict_reader:
                print(row)
                # print(row.get('user'), row.get('passwd'), row.get('date'))
    
    OrderedDict([('user', 'a1'), ('passwd', 'a2'), ('date', '2018-04-23')])
    OrderedDict([('user', 'b1'), ('passwd', 'b2'), ('date', '2018-04-24')])
    OrderedDict([('user', 'c1'), ('passwd', 'c2'), ('date', '2018-04-25')])
    

    可以看出区别一个是list, 一个是OrderedDict,在切换注释,使用get取值:

    a1 a2 2018-04-23
    b1 b2 2018-04-24
    c1 c2 2018-04-25
    

    使用DictReader()读取文件,会使用第一行的值当key和后面每一行组成一个OrderedDict,这样取值使用更方便

    • fieldnames 第一行字段名
        with open(path, "rt") as f:
            dict_reader = csv.DictReader(f)
    
            print(dict_reader.fieldnames)
    
    ['user', 'passwd', 'date']
    
    • 当数据不包含数据字段名时,可以指定字段名

    准备 csv 文件:

        a1,a2,2018-04-23
        b1,b2,2018-04-24
        c1,c2,2018-04-25
    

    代码如下:

        fieldnames = ['user', 'passwd', 'date']
        with open("csv_test.csv", "rt") as f:
            dict_reader = csv.DictReader(f, fieldnames=fieldnames)
    
            print(dict_reader.fieldnames)
            print("===========================")
            for row in dict_reader:
                print(row.get(fieldnames[0]), row.get(fieldnames[1]), row.get(fieldnames[2]))
    

    运行代码:

    ['user', 'passwd', 'date']
    ===========================
    a1 a2 2018-04-23
    b1 b2 2018-04-24
    c1 c2 2018-04-25
    

    三、写文件

    • writer()
        with open("csv_test.csv", "wt") as f:
            writer = csv.writer(f)
    
            writer.writerow(('user', 'passwd', 'date'))
    
            for i in range(3):
                writer.writerow((i + 1, chr(ord('a') + i), '2018-%02d-45' % (i+1)))
    
    • DictWriter()
        fieldnames = ['user', 'passwd', 'date']
        with open(path, "wt") as f:
            dict_writer = csv.DictWriter(f, fieldnames=fieldnames)
            headers = dict((n, n) for n in fieldnames)
            dict_writer.writerow(headers)
            for i in range(3):
                data = (i + 1, chr(ord('a') + i), '2018-%02d-45' % (i + 1))
                values = {}
                for index in range(len(fieldnames)):
                    values[fieldnames[index]] = data[index]
                dict_writer.writerow(values)
    

    DictWriter()需要单独写入文件头行

    四、逐行读取

    • reader()
        reader = csv.reader(open("csv_test.csv", "rt"))
    
        print(next(reader))
        print(next(reader))
        print(next(reader))
    
    • DictReader()
        def test(dict_reader):
            for row in dict_reader:
                yield row.get(fieldnames[0]), row.get(fieldnames[1]), row.get(fieldnames[2])
    
    
        dict_reader = csv.DictReader(open("csv_test.csv", "rt"))
        fieldnames = ['user', 'passwd', 'date']
        print(next(test(dict_reader)))
        print(next(test(dict_reader)))
        print(next(test(dict_reader)))
    
  • 相关阅读:
    Moodle 3.8 安装过程
    Discourse 数据分发
    Moodle 安装的时候提示 original IP
    Apache 安装运行测时候提示错误 (13)Permission denied: AH00091: httpd: could not open error log file
    Apache 配置 SELinux 命令的时候的命令 semanage
    Apache Httpd 安装 AH00558错误
    Discourse 如何修改用户自己的密码
    Discourse 如何限制注册用户的密码长度
    Discourse 的快捷键列表
    S3 存储附件和图片无法上传
  • 原文地址:https://www.cnblogs.com/wj5633/p/8944443.html
Copyright © 2020-2023  润新知