Python Forum
Matching between two files + using next()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Matching between two files + using next()
#3
How big are the two log files? Do they fit into memory?

If events do fit in memory, you can load all events into memory, strip newlines and put them into a set. A set has only unique elements and does not preserve the order.

The second file could be bigger because you can iterate line by line over the file object, which saves memory.

# naive approach

with open("f1.txt") as f1:
    events = set(line.strip() for line in f1)

with open("f2.txt") as f2:
    for f2_line in f2:
        for event in events:
            if event in f2_line:
                print(f2_line, end="")
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: Matching between two files + using next() - by DeaD_EyE - Jun-18-2020, 07:05 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Finding files matching pattern GrahamL 1 1,380 Jan-14-2022, 01:16 PM
Last Post: DeaD_EyE
  Compare filename with folder name and copy matching files into a particular folder shantanu97 2 4,671 Dec-18-2021, 09:32 PM
Last Post: Larz60+
  Matching two files based on a spited elements tester_V 5 2,888 May-30-2021, 07:49 PM
Last Post: tester_V
  How to get full path of specified hidden files matching pattern recursively SriRajesh 4 4,079 Jan-18-2020, 07:12 PM
Last Post: SriRajesh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020