簡體   English   中英

合並目錄中以分號分隔的txt文件循環

[英]Merge semicolon delimited txt file looping in directory

假設我來自同一目錄的許多不同文本文件,其內容結構如下所示:

文件a.txt:

HEADER_X;HEADER_Y;HEADER_Z
a_value;a_value;a_value
a_value;a_value;a_value

文件b.txt:

HEADER_X;HEADER_Y;HEADER_Z
b_value;b_value;b_value
b_value;b_value;b_value

文件c.txt:

HEADER_X;HEADER_Y;HEADER_Z
c_value;c_value;c_value
c_value;c_value;c_value

文件d.txt:...

我想通過將每個文件的內容附加到每個先前文件的最后一行,將所有txt文件合並為一個。 見下文:

文件Combine.txt:

HEADER_X;HEADER_Y;HEADER_Z
a_value;a_value;a_value
a_value;a_value;a_value
b_value;b_value;b_value
b_value;b_value;b_value
c_value;c_value;c_value
c_value;c_value;c_value
...

如何在Python中執行此操作?

假設:-所有txt文件都位於同一文件夾中-所有txt文件具有相同的標題-所有txt文件具有相同的列數-所有txt文件具有不同的行數

使用CSV模塊 像這樣:

import csv
with ('output.csv', 'ab') as output:
    writer = csv.writer(output, delimiter=";")

    with open('a.txt', 'rb') as csvfile:
        reader = csv.reader(csvfile, delimiter=";")
        reader.readline() // this is to skip the header
        for row in spamreader:
            writer.writerow(row)

如果您不想在每個文件中進行編碼(假設您有三個以上),則可以執行以下操作:

from os import listdir
from os.path import isfile, join
onlyfiles = [ f for f in listdir(mypath) if isfile(join(mypath,f)) ]
    for aFile in onlyfiles:
        with open(aFile, 'rb') as csvfile:
        reader = csv.reader(csvfile, delimiter=";")
        reader.readline() // this is to skip the header
        for row in spamreader:
            writer.writerow(row)

我設法做了似乎可行的事情(至少在我測試過的情況下)。 這將解析所有文件,獲取所有標頭,並對每個文件的每一行中的值設置格式以添加“;”。 根據該文件上存在/不存在的標題。

headers = []
values = []
files = ("csv0.txt", "csv1.txt")#put the files you want to parse here

#read the files a first time, just to get the headers
for file_name in files:
    file = open(file_name, 'r')
    first_line = True
    for line in file:
        if first_line:
            first_line = False
            for header in line.strip().split(";"):
                if header not in headers:
                    headers.append(header)
        else:
            break
    file.close()
headers = sorted(headers)

#read a second time to get the values
file_number = 0
for file_name in files:
    file = open(file_name, 'r')
    file_headers = []
    first_line = True
    corresponding_indexes = []
    values.append([])
    for line in file:
        if first_line:
            first_line = False
            index = 0
            for header in line.strip().split(";"):
                while headers[index] != header:
                    index += 1
                corresponding_indexes.append(index)
        else:
            line_values = line.strip().split(";")
            current_index = 0
            values_str = ""
            for value in line_values:
                #this part write the values with ";" added for the headers not in this file
                while current_index not in corresponding_indexes:
                    current_index += 1
                    values_str += ";"
                values_str += value + ";"
                current_index += 1
            values_str = values_str[:-1] #we remove the last ";" (useless)
            values[file_number].append(values_str)
    file_number += 1
    file.close()

#and now we write the output file with all headers and values
headers_str = ""
for header in headers:
    headers_str += header + ";"
headers_str = headers_str[:-1]
output_file = open("output.txt", 'w')
output_file.write(headers_str + "\n")
for file_values in values:
    for values_line in file_values:
        output_file.write(values_line + "\n")
output_file.close()

如果您有任何疑問,請隨時提問。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM