[英]Python CSV writer - writing columns in new csv file up to maximum number of fields in csv files
我的文件夾中有200個CSV文件。 我想做的是讀取每個文件的第一行並寫入新的csv。 最重要的是,我想寫[file,field1,field2,... fieldn] n是最大字段數。
import csv
import glob
list=[]
hel=[]
files=glob.glob('C:/dataset/*.csv')
with open('test.csv', 'w',newline='') as testfile:
csv_writer = csv.writer(testfile)
for file in files:
with open(file, 'r') as infile:
file=file[file.rfind('\\')+1:]
file=file.strip('.csv')
reader = csv.reader(infile)
headers = next(reader)
hel.append((len(headers)))
max(hel)
lst = [file] + headers
csv_writer.writerow(lst)
結果表明,200個文件的最大字段數為255。因此,在新的csv文件上,我想寫入file, field1, field2 ... field 255.
我該怎么做?
import csv
import glob
list=[]
hel=[]
files=glob.glob('C:/dataset/*.csv')
with open('test.csv', 'w',newline='') as testfile:
csv_writer = csv.writer(testfile)
for file in files:
with open(file, 'r') as infile:
file=file[file.rfind('\\')+1:]
file=file.strip('.csv')
reader = csv.reader(infile)
headers = next(reader)
hel.append((len(headers)))
b=['field{}'.format(i) for i in range(1,max(hel)+1)]
lst = [file] + headers
csv_writer.writerow(lst)
現在b
是看起來像這樣的列表['field1','field2'...'field255']我需要在'field1'之前插入'file'並將該行寫在新的csv文件的頂部。 在csv_writer.writerow(lst)
之后編寫代碼會給我帶有'field1','field2'..
每隔一行的csv文件。 我該如何解決這個問題
首先,您需要讀取所有輸入文件以確定最大字段數為255。然后,您需要構造一個字段名列表以寫入輸出文件(僅一次,而不是循環):
['field{}'.format(i) for i in range(1, 256)]
您可以將該列表傳遞給csv
模塊以編寫它。
在寫入文件之前,請從每個文件中讀取字段計數和第一行。
import glob
from itertools import chain
import os
from os.path import splitext, basename
def first_line(filepath):
with open(filepath) as f:
return next(f)
def write_test_file(dest_file_path, source_path_name):
source_paths = glob.glob(source_path_name)
first_lines = list(map(first_line, source_paths))
max_count = max(l.count(",") for l in first_lines)
field_names = map("field{}".format, range(1, max_count + 2))
header = ",".join(chain(["file"], field_names)) + os.linesep
file_names = (splitext(basename(p))[0] for p in source_paths)
content = chain([header], map(",".join, zip(file_names, first_lines)))
with open(dest_file_path, 'w') as testfile:
testfile.write("".join(content))
write_test_file('test.csv', 'C:/dataset/*.csv')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.