读取csv文件
csv即为逗号分割之,有时也称为字符分割值,其文件以纯文本形式存储表格数据(数字和文本)
1、csv文件读取
案例:读取stu_info.csv文件里所有学生信息
import csv
csv_file=csv.read(open('stu_info.csv','r'))
for stu in csv_file : #for循环遍历这个表的每一行
print(stu)
2、csv文件写入
对Stu_info.csv文件追加写入两个学生信息Marry和Rom
stu=['Marry',28,'shanghai']
stu1=['Rom',23,'beijing']
out=open('Stu_info.csv','a',newline=' ') #打开文件 a是追加
#设定写入模式 (out:传入写入模式,dialect:定义文件类型,是exel)
csv_write=csv.writer(out,dialect='excel')
csv_write.writerow(stu) #写入具体内容
csv_write.writerow(stu1)
print("write file over")