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
CodePudding user response:
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
...