csv模块读写,map映射namedtuple处理csv快速

csv模块处理csv文件,不用pandas之前还是可行的,接口自动化namedtuple命名元组处理csv文件会更快速。结合ddt数据驱动更变快。元组本身特性静态储存,性能会快一些吧

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
===========================
# @Time : 2020/9/23 22:10
# @File  : csv01.py
# @Author: adeng
# @Date  : 2020/9/23
============================
"""

import csv,os

# csv读取
def csv_reader(filepath, read_start_line=False):
    if os.path.isfile(filepath):
        data = []
        with open("table.csv",mode="r") as f:
            if not read_start_line:
                header = f.readline().strip().split(",") # 跳过第一行,因为读取了,seek光标在第2行开头。
            datas = csv.reader(f)
            for d in datas:  # 读取d是一个列表
                data.append(d)
            return data
    else:
        raise TypeError(f"你传入的路径{filepath}不是一个文件")


# datas = csv_reader("table.csv")
# print(datas)

# ---------------namedtuple----处理csv文件

from collections import namedtuple


with open("table.csv",mode="r") as f:
    header = f.readline().strip().split(",")
    print(header[0])
    EmployeeRecord = namedtuple('EmployeeRecord', header)
    m = map(EmployeeRecord._make,csv.reader(f))
    print(list(m))
    for index,i in enumerate(m):
        print(i)
        for h in header:
            print("\t",getattr(i,h))


#------namedtuple处理csv文件-----------

def get_csv_namedtuple(filepath):
    with open(filepath, mode="r") as f:
        header = f.readline().strip() # 这里读取一行,f是生成器。之后再用会少一行
        EmployeeRecord = namedtuple('EmployeeRecord', header)
        return tuple(map(EmployeeRecord._make, csv.reader(f)))

m = get_csv_namedtuple(r"table.csv")

#-------------写入csv文件——writerow()- writerows()

import csv
# newline="" 消除没写入一行会出现一行空行效果
# writerow()参数是一个列表,每次写入一行
with open("csv001.csv",mode="w",encoding="utf-8",newline="") as f:
    my_writer = csv.writer(f)
    my_writer.writerow(["name","age"])
    my_writer.writerow(["阿登",18])
    my_writer.writerow(["向佳",20])
    # writerows()参数是一个嵌套列表的列表,元素有多少个列表就写入多少行
    my_writer.writerows([["阿登",18],["阿登",18],["阿登",18]])

你可能感兴趣的:(csv模块读写,map映射namedtuple处理csv快速)