zoukankan      html  css  js  c++  java
  • 第8课-python实现csv文件读写

    1、记录python读写csv文件,直接上代码

    import csv
    
    header = ["学号","姓名","年龄"]
    
    values = [
        ("1","zhangsan","19"),
        ("2","lisi","20"),
        ("3","wangwu","22"),
        ]
    
    def read_csv_file():
        with open("test.csv","r",encoding="utf-8") as fp:
            # reader = csv.reader(fp) #使用列表的方式读取
            reader = csv.DictReader(fp) #使用字典的方式读取
            for x in reader:
                print(x)
    
    def write_csv_file():
        with open("classroom.csv","w",encoding="utf-8",newline="") as fp:
            writer = csv.writer(fp)
            writer.writerow(header)
            writer.writerows(values)
    
    
    def write_csv_file_dic():
        values = [
            {"学号":100,"姓名":"高文祥","年龄":18},
            {"学号":101,"姓名":"刘德华","年龄":28},
            {"学号":102,"姓名":"李连杰","年龄":38}
        ]
        with open("classroom1.csv","w",encoding="utf-8",newline="") as fp:
            writer = csv.DictWriter(fp,header)
            writer.writeheader()
            writer.writerows(values)
    if __name__ == '__main__':
        write_csv_file_dic()
    

      

  • 相关阅读:
    9.19 数组 冒泡排序和二分法
    9,18 考试
    html5学习拓展
    第七天,
    第六天 js 开始
    第五天 列表 流
    第四天 样式,框架
    硬件攻城狮设计需要考量的11个关键因素
    步进电机的驱动方式
    MOS管防反接电路设计
  • 原文地址:https://www.cnblogs.com/win0211/p/12108346.html
Copyright © 2011-2022 走看看