Python Forum
Splitting lines ang grouping three at once
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Splitting lines ang grouping three at once
#1
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
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
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.
Reply
#6
Hoping someone will read one of my posts some day.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Grouping Data based on 30% bracket purnima1 4 1,188 Mar-10-2023, 07:38 PM
Last Post: deanhystad
  Grouping and sum of a list of objects Otbredbaron 1 3,194 Oct-23-2021, 01:42 PM
Last Post: Gribouillis
  splitting lines, need to add element of same kind tester_V 6 3,121 Feb-20-2021, 11:51 PM
Last Post: tester_V
  Grouping and summing of dataset jef 0 1,633 Oct-04-2020, 11:03 PM
Last Post: jef
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 5,808 Aug-10-2020, 11:01 PM
Last Post: medatib531
  Grouping algorithm riccardoob 7 2,987 May-19-2020, 01:22 PM
Last Post: deanhystad
  Help Grouping by Intervals on list paul41 1 2,104 Dec-03-2019, 09:43 PM
Last Post: michael1789
  Grouping a list of various time into intervals paul41 1 3,767 Nov-24-2019, 01:47 PM
Last Post: buran
  resample grouping pr0blem olufemig 1 1,945 Nov-06-2019, 10:45 PM
Last Post: Larz60+
  Grouping csv by name terrydidi 8 3,896 Jan-14-2019, 09:27 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020