只是一个普通的 .csv 文件 第一行有每列的标题。
我想知道如何创建一个具有相同标题(第一行)但包含原始文件的每 5 行的新 .csv 文件?
谢谢你!
这将获取任何文本文件并输出第一行和之后的每第五行。如果未访问列,则不必将其作为 .csv 进行操作:
with open('a.txt') as f:
with open('b.txt','w') as out:
for i,line in enumerate(f):
if i % 5 == 0:
out.write(line)
这将一次读取文件一行,仅写入第 5、10、15、20... 行
import csv
count = 0
# open files and handle headers
with open('input.csv') as infile:
with open('ouput.csv', 'w') as outfile:
reader = csv.DictReader(infile)
writer = csv.DictWriter(outfile, fieldnames=reader.fieldnames)
writer.writeheader()
# iterate through file and write only every 5th row
for row in reader:
count += 1
if not count % 5:
writer.writerow(row)
(使用 Python 2 和 3)
如果您希望从数据行 #1 开始写入第 1、6、11、16... 行,请在顶部更改为:
count = -1
如果你想使用 csv 库,更严格的版本将是......
import csv
# open files and handle headers
with open('input.csv') as infile:
with open('ouput.csv', 'w') as outfile:
reader = csv.DictReader(infile)
writer = csv.DictWriter(outfile, fieldnames=reader.fieldnames)
writer.writeheader()
# iterate through file and write only every 5th row
writer.writerows([x for i,x in enumerate(reader) if i % 5 == 4])