编程语言
首页 > 编程语言> > Python笔记-csv文件的读写

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

标签:20,writerow,Python,读写,file,王五,100,csv
来源: https://blog.csdn.net/qq_44782285/article/details/112972011