Read txt file from specific of line to a certain line base on string

Question:

I am trying to write some function of data, however, my data is like this:

noms sommets
0000 Abbesses
0001 Alexandre Dumas
0002 Paris
0004 Nice
...
coord sommets
0000 308 536
0001 472 386
0002 193 404

What I want to is to access from nom sommets to 0004 Nice without knowing the number of line but base on the string value of txt

Asked By: Beam291

||

Answers:

Read text file to list split based on key word sommets

with open('text.txt') as file:
    lines = [line.rstrip() for line in file]

test_loop = []
for item in lines:
    if 'sommets' in item: 
        test_loop.append([item])
    else:  
        test_loop[-1].append(item)
print(test_loop)

which gives list of lists #.

[['noms sommets', '0000 Abbesses', '0001 Alexandre Dumas', '0002 Paris', '0004 Nice', '...'], ['coord sommets', '0000 308 536', '0001 472 386', '0002 193 404']]

If you want to access first set then.

for sublist in test_loop[0]:
    print(sublist)

Gives #

noms sommets
0000 Abbesses
0001 Alexandre Dumas
0002 Paris
0004 Nice
...
Answered By: Bhargav

we can read the whole txt file as single string then use regex to match the keyword.

re.DOTALL modifier will make ‘.’ match any character including newline

with open('text.txt') as f:
  txt = f.read()

match = re.search('noms sommets.*?0004 Nice', a, re.DOTALL).group()
# match = 'noms sommetsn0000 Abbessesn0001 Alexandre Dumasn0002 Parisn0004 Nice'

then, you can use match as variable or split it into list

>>> print(match)
noms sommets
0000 Abbesses
0001 Alexandre Dumas
0002 Paris
0004 Nice
>>>
Answered By: Skycc
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.