簡體   English   中英

使用 python 將具有相似值的文本文件合並到一個文件中

[英]Combining text files with similar values into one file using python

我已經搜索了該站點,但找不到與我試圖完成的任務完全相同的任何內容。 我有 2 個文本文件,我想根據每個文件中的第一行將它們合並到一個文件中(讓我們將此行稱為 x)。 例如,如果 x 存在於 file1 和 file2 中,那么我想獲取 x 並在其行上顯示來自 file1 和 file2 的處理信息。 注意,file1 包含一個 header。 以下是每個文件讀取方式的預覽:

文件 1:

X, DES1, DES2, DES3, NUMBERS
123, text, text, text, 456
321, text, text, text, 43222
124, text, text, text, 3254
125, text, text, text, 2352634
279, text, text, text, 3243
567, text, text, text, 00001
345, text, text, text, 02

文件 2:

123, 152352364
124, 32535
125, 745734
345, 4000 

等等。 file2 中的每個元素(或 x)都存在於 file1 中。 但是,file1 包含 file2 中沒有的其他 x 值。 我仍然可以將兩個文件中的數據合並到一個新文件中嗎? 以下是我嘗試過的,但在我的打印語句中出現 KeyError。 我確定代碼非常錯誤,僅供參考。

f1 = {}
with open ("file1.txt") as my1:
    for line in my1.readlines():
        f1[line.split(",")[0]] = line.strip().split(",")[1:]

f2={}
with open ("file2.txt") as my2:
    for line in f.readlines():
        f2[line.split(",")[0]] = line.strip().split(",")[1:]

for key in f1.keys():
    print(key, str.join(",",f1[key]), str.join(",",f2[key]))

任何幫助,將不勝感激。 我知道我可能不得不大量返工或廢棄我目前擁有的東西。 我預期的 output 如下所示:

X, DES1, DES2, DES3, NUMBERS, NEWNUMB        
123, text, text, text, 456, 152352364    
321, text, text, text, 43222, 0    
124, text, text, text, 3254, 32535    
125, text, text, text, 2352634, 745743    
279, text, text, text, 3243, 0    
567, text, text, text, 00001, 0    
345, text, text, text, 02, 4000    

您沒有從file1.txt跳過 header 行

f1 = {}
with open ("file1.txt") as file1:
    next(file1)  # skip the header (first line)
    for line in file1:  # for loop iterates over lines by default
        f1[line.split(",")[0]] = line.strip().split(",")[1:]

f2 = {}
with open ("file2.txt") as file2:
    for line in file2:
        f2[line.split(",")[0]] = line.strip().split(",")[1:]


# generate the contents of the new file
lines = [
    ['X', 'DES1', 'DES2', 'DES3', 'NUMBERS', 'NEWNUMB']  # headings
]
for key, value in f1.items():
    # get will return the second argument if the key doesn't exist
    new_num = f2.get(key, ['0'])
    # unpack the values into a new list and append it to lines
    lines.append([key, *value, *new_num])

for line in lines:
    print(','.join(line))

您需要對代碼進行更多必要的更改。 你應該玩弄它並嘗試自己做。 我只是修復了錯誤。

disciple@diptangsu:~/Desktop/sample$ cat file1.txt 
X, DES1, DES2, DES3, NUMBERS
123, text, text, text, 456
321, text, text, text, 43222
124, text, text, text, 3254
125, text, text, text, 2352634
279, text, text, text, 3243
567, text, text, text, 00001
345, text, text, text, 02
disciple@diptangsu:~/Desktop/sample$ cat file2.txt 
123, 152352364
124, 32535
125, 745734
345, 4000 
disciple@diptangsu:~/Desktop/sample$ python3 code.py 
X,DES1,DES2,DES3,NUMBERS,NEWNUMB
123, text, text, text, 456, 152352364
321, text, text, text, 43222,0
124, text, text, text, 3254, 32535
125, text, text, text, 2352634, 745734
279, text, text, text, 3243,0
567, text, text, text, 00001,0
345, text, text, text, 02, 4000

如果你不知道next是什么,我建議你閱讀 python 中的生成器。

暫無
暫無

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

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