在合并多个CSV文件时,我可以从所有文件中获取标头,也可以完全不获取标头。我希望仅来自第一个文件的标头,因为所有文件都具有相同的标头,并且我正在合并列下面的列。
我是python的新手。实际上,在不同的子文件夹中有23个同名CSV文件。我正在使用循环逐行读取它们。从那我只想要第一个文件头。
这是我的代码:
import os, sys
`import pathlib
# Specify directory
# In your case, you may want something like the following
my_directory = 'C:/Users/Arijeet/Downloads'
file = pathlib.Path("out.csv")
if file.exists ():
print("file found\nremoving")
os.remove('out.csv')
else:
print("file not find\ncreating")
counter = 1
# Start the loop
for folder, sub_folders, files in os.walk(my_directory):
for special_file in files:
if special_file == 'iono_tropo.csv':
file_path = os.path.join(folder, special_file)
# Open and read
with open(file_path) as read_file:
print('Reading iono_tropo csv file ' + str(counter))
lines=read_file.readlines()
with open ("out.csv","a+") as f:
f.writelines(lines)
counter += 1
我该怎么办?
答案 0 :(得分:0)
我不太清楚您的意思,但是如果您要合并某些csv并仅保留头中的头,就应该这样做。
with open ("file1.csv", "r") as file:
data = file.readlines()
data[-1] += "\n" #Otherwise data from next file will be on the same line
for filename in ["file2.csv", "file3.csv", "file4.csv", "file5.csv"]:
with open(filename, "r") as file:
file.readline() #Skips the header for all the other files
data += file.readlines()
data[-1] += "\n" #Otherwise data from next file will be on the same line
#Creating the merged file
with open("merged.csv", "w") as merged:
for line in data:
merged.write(line)