繁体   English   中英

从下一个文件中跳过标题

[英]Skip header from next file

合并多个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

我能做什么?

我不完全理解您的意思,但是如果您要合并某些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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM