I have searched the site but can't find anything exactly similar to what im trying to accomplish. I have 2 text files that I want to merge into 1 file based on the first row in each file (lets call this row x). For example, if x exists in file1 and file2 then I want to take x and display the proceeding info from file1 and file2 on its line. Note, file1 contains a header. Below is a preview of how each file reads:
File 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
File 2:
123, 152352364
124, 32535
125, 745734
345, 4000
And so on. Each element(or x) in file2 exists in file1. However, file1 contains other values for x that are not in file2. Can I still combine the data from the two files together in a new file? Below is what I tried but I get a KeyError on my print statement. Im sure the code is very wrong, FYI.
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]))
Any help would be appreciated. I understand i will likely have to heavily rework or scrap what I have so far. My expected output would look as follows:
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