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
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)
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)
RE: Splitting lines ang grouping three at once - ichabod801 - Jun-21-2019 Hoping someone will read one of my posts some day. |