python3从第二行开始读取csv
2022/1/24 20:06:24
本文主要是介绍python3从第二行开始读取csv,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
python3从第二行开始读取csv
- 实例代码
- 将读取的信息映射到字典
- 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)
这篇关于python3从第二行开始读取csv的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python