I have a list of line numbers in a text file.
linenrs = [8,12,18,21]
I want to join all lines from one line number to the next line number.
This is what I've created and works.
newlist, templist = [], []
counter = 0
separator = " "
for i in range(1, 24): #23 lines in textfileline
temps = texfileline(i)
if i in linenrs:
if counter == 0:
templist.append(temps)
counter = 1
elif counter == 1:
newlist.append(separator.join(templist))
templist = []
templist.append(temps)
counter = 1
elif counter == 1 and i <= linenrs[-1]:
templist.append(temps)
else:
newlist.append(temps)
Is this possible with list manipulation?
I would like to obtain such a list (and then join the text of these lines in the textfile):
newlist = [[8,9,10,11], [12,13,14,15,16,17], [18,19,20], [21,22,23]]
Using itertools.islice with specific ranges is a good choice for such task.
Suppose we have an input file with structure
<consecutive number><text>
:The output (sublists of lines):