I want to force a line break after every 10 numbers or 9 nine splits on a txt file in python? How would I go about this? So say i have
Input would be something like:
40 20 30 50 40 40 40 40 40 40
20 40 20 30 50 40 40 40
20 40 20 30 50 40 40 40 40 20 20 20
20 20 20
int a txt.file and output should be
40 20 30 50 40 40 40 40 40 40
20 40 20 30 50 40 40 40
20 40 20 30 50 40 40 40 40 20
20 20
20 20 20
so essentially break after every 10 numbers, or 9 line splits
I have tried:
with open('practice.txt') as f:
for line in f:
int_list = [int(num) for num in line.split() ]
if len(int_list) < 20:
print(int_list)
else:
new_list = int_list[20:]
int_list = int_list[:20]
print(int_list)
print(new_list)
But this doesn't quite solve it. Also note that the line lengths can vary. So the first line could have 5 numbers and the second line have 9 and the third 10 and the fourth 10