python用列表形式读取CSV文件
语法:csv.reader(f, delimiter=‘,’)
reader为生成器,每次读取一行,每行数据为列表格式,可以通过delimiter参数指定分隔符
import csv
with open('information.csv',encoding='utf-8')as fp:
reader = csv.reader(fp)
# 获取标题
header = next(reader)
print(header)
# 遍历数据
for i in reader:
print(i)
读取文件时文件路径务必要写对,不确定时可写绝对路径。要获取csv的内容则需要遍历再输出。