Python Forum
Find specific subdir, open files and find specific lines that are missing from a file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find specific subdir, open files and find specific lines that are missing from a file
#8
tester_V Wrote:Could you explain the code please?
Well the expresssion re.search(r"CELL\-(\d+)", line) returns either None or a MatchObject in the sense of the re module. There is a match object if a substring such as CELL-8 was found in the line. With the match object, one can get the number 8, that's the value returned by the expression int(match.group(1)). Thus the sequence
sequence = (re.search(r"CELL\-(\d+)", line) for line in cells_file)
is a sequence such as None, None, match, None, match,... with one item per line of the file.

The expression
inum = set(int(match.group(1)) for match in sequence if match)
computes the set of all integers found in the above matches, hence all the integers i such that CELL-i was found in the file (actually, only the first occurrence on each line is taken into account).
Then there is a loop, equivalent to
for i in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]:
    if i in inum:
        print(f"CELL-{i} infile")
    else:
        print(f"CELL-{i} missing")
Reply


Messages In This Thread
RE: Find specific subdir, open files and find specific lines that are missing from a file - by Gribouillis - Aug-24-2020, 07:11 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Right way to open files with different encodings? Winfried 2 297 Apr-23-2024, 05:50 PM
Last Post: snippsat
  Open files in an existing window instead of new Kostov 2 371 Apr-13-2024, 07:22 AM
Last Post: Kostov
  delete specific row of entries jacksfrustration 3 434 Feb-13-2024, 11:13 PM
Last Post: deanhystad
  Extracting specific file from an archive tester_V 4 564 Jan-29-2024, 06:41 PM
Last Post: tester_V
  Open/save file on Android frohr 0 356 Jan-24-2024, 06:28 PM
Last Post: frohr
  Why can't I copy and past only ONE specific tab? NewWorldRonin 8 900 Jan-12-2024, 06:31 PM
Last Post: deanhystad
  data validation with specific regular expression shaheen07 0 366 Jan-12-2024, 07:56 AM
Last Post: shaheen07
  file open "file not found error" shanoger 8 1,242 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Can't Find Python (or Something) pklind 2 569 Nov-26-2023, 11:11 AM
Last Post: snippsat
  Can't Find Path hatflyer 8 1,148 Oct-30-2023, 06:17 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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