python3从第二行开始读取csv
参考: https://geek-docs.com/python/python-tutorial/python-csv.html
有时候csv第一行会作为注释信息,而读取配置时不需要读取第一行
实例代码
def read_n2_csv(filepath):
"""
从第2行开始读取csv文件
:param filepath:
:return:
"""
with open(filepath) as f:
reader = csv.reader(f)
next(reader)
for i in reader:
print(i)
将读取的信息映射到字典
def readcsv_file(filepath):
"""
读取csv文件
:param filepath: 传入文件路径
:return:
"""
with open(filepath) as f:
reader = csv.DictReader(f)
for row in reader:
# 这里传入首行的key
print(row['commit'], row['platform'], row['workload'])
csv写入数据
import csv
# 要写入的数据
nms = [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]]
with open(filepath, 'w') as f:
writer = csv.writer(f)
for row in nms:
writer.writerow(row)