1

I have two files, let's say File A and B. And I want to get a specific value after the word "Query" in File A and find that value in File B, which is a large file. After which if it matches, the python program will print "Sorry, item already exist".

One caveat is that File B is exported from a platform, so gibberish/additional data will be present.

An example of the files can be seen as shown below:

File A:

{"ID": "01", "Name": "..", "Query": "zzzzzzzzzzz"}{"ID": "02","Name": "..", "Query": "xxxaaaaxxxx"}

File B

[_some fileheader info]{"ID": "...", "Name": "..", "Query": "kkkkkkkkkk"}{"ID": "...", "Name": "..", "Query": "xxxxxxxxx" }{"ID": "...", "Name": "..", "Query": "zzzzzzzzzzz"}{"ID": "...", "Name": "..", "Query": "pppppppppp"}

So the expected outcome will be printed - "Entry ID 01 already exist in File B!"

This is what I am working on (am unsure how to specifically search for "Query"), but doesn't seem to work:

    with open('FileA') as f1:
        with open('FileB') as f2:
              if f1.read() in f2.read():
                 print("Entry already exist!")
              else:
                 print("Importing entry....")
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
XynoYoo__
  • 5
  • 5
  • So you need to match _each_ `"Query": ""` from FileA _anywhere_ in FileB? The order and the surrounding texts doesn't matter? – Gino Mempin Aug 16 '22 at 08:58
  • @GinoMempin Yes! That's what I am trying to achieve. And yes, the order doesn't matter. – XynoYoo__ Aug 16 '22 at 09:12
  • Does FileA follow any specific format? Based on your example, it doesn't look like JSON, and I think the trickier part here is parsing all the `"Query": ""` and putting them all into a list. Once they're all on a list, then it's just a matter of applying existing solutions to [find a text in a large file](https://stackoverflow.com/q/3893885/2745495). – Gino Mempin Aug 16 '22 at 09:16
  • @GinoMempin oh, its in ndjson file format. Will this make things trickier to put in a list? – XynoYoo__ Aug 16 '22 at 09:30

1 Answers1

-1
with open('FileA') as f1:
    with open('FileB') as f2:
        for i in f1.readline().split():
            for b in f2.readline().split():
                if i in b:
                    print('Yes')
                    break