Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
searching contents of files
#10
with open("a.txt") as master_file:
    keywords = {line.strip() for line in master_file} # this is a set comprehension
    # https://docs.python.org/3/library/stdtypes.html#types-set
    # https://docs.python.org/3/tutorial/datastructures.html#sets
    # this gives you an unique set of the lines in master_file
    # line.strip() removes newlines and whitespace
    # all happens inside memory, if the file is bigger than your memory, this doesn't work

# here you can work with keywords.
# just iterate over the second file, split the line, select the right field and look if this field
# is in keyweords, if True, then do something with the information
# like writing it to a third file
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
searching contents of files - by Ahmedrhle - Jul-19-2017, 08:48 PM
RE: searching contents of files - by nilamo - Jul-19-2017, 08:52 PM
RE: searching contents of files - by Ahmedrhle - Jul-19-2017, 08:55 PM
RE: searching contents of files - by nilamo - Jul-19-2017, 08:57 PM
RE: searching contents of files - by Ahmedrhle - Jul-19-2017, 09:13 PM
RE: searching contents of files - by nilamo - Jul-19-2017, 09:29 PM
RE: searching contents of files - by Ahmedrhle - Jul-19-2017, 09:37 PM
RE: searching contents of files - by nilamo - Jul-19-2017, 09:39 PM
RE: searching contents of files - by Ahmedrhle - Jul-19-2017, 09:50 PM
RE: searching contents of files - by DeaD_EyE - Jul-20-2017, 09:30 AM
RE: searching contents of files - by Ahmedrhle - Jul-20-2017, 02:32 PM
RE: searching contents of files - by Ahmedrhle - Jul-20-2017, 05:22 PM
RE: searching contents of files - by DeaD_EyE - Jul-20-2017, 07:09 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  splitting file into multiple files by searching for string AlphaInc 2 985 Jul-01-2023, 10:35 PM
Last Post: Pedroski55
  Searching for specific word in text files. JellyCreeper6 1 1,775 Nov-03-2020, 01:52 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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