Python笔记-csv文件的读写

import csv
file = open('demo.csv','w',encoding='utf8',newline='')
w = csv.writer(file)
w.writerow(['name','age','score','city'])
# w.writerow(['张三','20','100','上海'])
# w.writerow(['李四','22','100','武汉'])
# w.writerow(['王五','25','100','北京'])
w.writerows(
    [
        ['张三','20','100','上海'],
        ['李四','22','100','武汉'],
        ['王五','25','100','北京'],
    ]
)
file.close()

file = open('info.csv','r',encoding='utf8')
r = csv.reader(file)
for data in r:
    print(data)
file.close()

结果

[]
['姓名,年龄,性别,城市']
['张三,22,男,上海']
['李四,20,男,北京']
['王五,30,女,武汉']
['tony', '25', '女,南京']
['tom', '26,女,深圳']

Process finished with exit code 0
上一篇:go基本使用方法


下一篇:CSS3之3D轮播图