Python Forum
Splitting lines ang grouping three at once - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Splitting lines ang grouping three at once (/thread-19286.html)



Splitting lines ang grouping three at once - samsonite - Jun-21-2019

What I've done, up to now, is successful only for the first group of short.txt database, while my goal is to have some drifts on adjusting iteratively the line tle= data[0]+'\n'+data[1]+'\n'+data[2]+'\n' in order to complete the whole file scanning.
Thx in advance

# --- line6.py ------
with open ("short.txt", "r") as myfile:
    data = myfile.read().splitlines()
tle= data[0]+'\n'+data[1]+'\n'+data[2]+'\n'
print(tle)
#------ OUTPUT ----------    
'''
FALCON 9 DEB
1 44298U 19029BR  19167.86418532  .00001313  00000-0  38063-4 0  9991
2 44298  53.0043  62.2948 0004912  13.4119 346.7015 15.43512957  4901
'''
short.txt
Output:
FALCON 9 DEB 1 44298U 19029BR 19167.86418532 .00001313 00000-0 38063-4 0 9991 2 44298 53.0043 62.2948 0004912 13.4119 346.7015 15.43512957 4901 STARLINK AB 1 44260U 19029AB 19157.99245370 .01269439 00000-0 29305-1 0 9993 2 44260 53.0001 109.7412 0006821 81.1213 255.2409 15.39387046 2245 STARLINK E 1 44239U 19029E 19167.13356954 -.00013522 00000-0 -91175-3 0 9994 2 44239 53.0083 69.0416 0002637 113.5217 246.6049 15.05529924 3542 STARLINK P 1 44248U 19029P 19167.86126092 -.00617922 00000-0 -42907-1 0 9993 2 44248 52.9974 64.8224 0004816 42.9633 317.1731 15.07410453 3682 STARLINK D 1 44238U 19029D 19167.85872376 -.00444761 00000-0 -30673-1 0 9990 2 44238 52.9982 64.8538 0002160 82.0309 278.0925 15.07669053 2123 STARLINK H 1 44242U 19029H 19167.72360443 -.01130229 00000-0 -86918-1 0 9993 2 44242 52.9937 65.5606 0008561 56.4128 303.7676 15.04350216 3717



RE: Splitting lines ang grouping three at once - ichabod801 - Jun-21-2019

Note that data[index:(index + 3)] gives the three item list starting at index. You can make a simple loop with range(0, len(data), 3) that gets every three item slice of the list.


RE: Splitting lines ang grouping three at once - samsonite - Jun-21-2019

Error erupts

# --- line6bis.py ------
with open ("short.txt", "r") as myfile:
    data[index:(index + 3)] = myfile.read().splitlines()
tle= data [range(0, len(data), 3)]
print(tle)
Error:
C:\Training>python line6bis.py Traceback (most recent call last): File "line6bis.py", line 3, in <module> data[index:(index + 3)] = myfile.read().splitlines() NameError: name 'data' is not defined



RE: Splitting lines ang grouping three at once - ichabod801 - Jun-21-2019

First of all, you didn't make the loop I mentioned, which would come after the file is read. Before the loop you initialize an empty list, and then in the loop you append the slide I mentioned to the new list.


RE: Splitting lines ang grouping three at once - samsonite - Jun-21-2019

Last tentative ... hoping someone helps by writing the solution. Thank you, forum!
# --- line6ter.py ------
with open ("short.txt", "r") as myfile:
	data = ""	
index=0
data[index:(index + 3)] = myfile.read().splitlines()
tle= data [range(0, len(data), 3)]
print(tle)
Error:
C:\Training>python line6ter.py Traceback (most recent call last): File "line6ter.py", line 7, in <module> data[index:(index + 3)] = myfile.read().splitlines() ValueError: I/O operation on closed file.



RE: Splitting lines ang grouping three at once - ichabod801 - Jun-21-2019

Hoping someone will read one of my posts some day.