Search code examples
python-3.xfilereadlines

Reading file line by line from #1comment till #2nd comment


i want to read, parse a .txt file and save it in a variables. After first comment #a till end of #~~ to variable a then for var b: to read after comment #b till comment #~~ and so on.

#a
  60   8   10
  12  30   12
#~~
#b  
14 2
30 12
#~~
#c
 40 14 

with open("file.txt") as f:
    for line in f:
         li = line.strip()
         if li.startswith("#a"):
    

Solution

  • I might just read the entire file into Python, and then use re.findall to find all comments:

    inp = """#a
      60   8   10
      12  30   12
    #~~
    #b  
    14 2
    30 12
    #~~
    #c
     40 14
    #~~"""
    
    matches = re.findall('#(\\w+)\s+(.*?)#~~\s*', inp, flags=re.DOTALL)
    print(matches)
    

    This prints:

    [('a', '60   8   10\n  12  30   12\n'), ('b', '14 2\n30 12\n'), ('c', '40 14\n')]
    

    In place of the hard-coded data I have above, use the following to read from your actual file:

    file = open("file.txt")
    inp = file.read()
    file.close()
    

    Edit:

    If you only want to retain the comment data itself, but not the labels, then reduce the 2D list to a 1D list via a comprehension:

    matches = re.findall('#(\\w+)\s+(.*?)#~~\s*', inp, flags=re.DOTALL)
    output = [j for (i, j) in matches]
    print(output)
    

    This prints:

    ['60   8   10\n  12  30   12\n', '14 2\n30 12\n', '40 14\n']