Home > Software design >  Parse file data in python
Parse file data in python

Time:06-22

I have two txt files with informations like:

file1.txt contains:

confapi,Last Build on :2022-Jun-16 13:53:45
downtstreamjob1forTest,Last Build on :2022-Jun-06 14:21:14
downtstreamjob2forTest,Last Build on :2022-Jun-06 14:21:11

file2.txt contains:

confapi
downtstreamjob2forTest

I want to remove to the entries in file1 that are present in file2 with the timestamp information.

output txt file:

downtstreamjob1forTest,Last Build on :2022-Jun-06 14:21:14

file2 does not contain timestamp information it contains only the job names.

I tried doing it with difference but no luck:

result = set(file1_words).difference(set(file2_words))

Please do not -1.

CodePudding user response:

You could try this:

FILE1 = 'file1.txt'
FILE2 = 'file2.txt'
OUTPUT = 'output.txt'

with open(FILE1) as file1, open(FILE2) as file2, open(OUTPUT, 'w') as output:
    keys = set(map(str.strip, file2))
    for line in file1:
        # check for blank line
        kw, *_ = line.split(',')
        if kw.strip() and kw not in keys:
            output.write(line)

CodePudding user response:

try this:

with open("file2.txt",'r') as fp:
    toremove = fp.readlines()
with open("file1.txt",'r') as fp:
    lines = fp.readlines()
to_insert = [x for x in lines if lines.split(",")[0] not in toremove]
with open("output.txt",'w') as fo:
    fo.writelines(to_insert)
  • Related