0

I have a text file called Orbit 1 and I need help opening it and then creating three separate arrays. I'm new to Python and have been having difficulty with this aspect. Here are the first few rows of my text file. There are 1112 rows including the header.

Year    Month   Day Hour    Minute  Second  Millisecond Longitude   Latitude    Altitude
2019    3   17  5   55  55  0   108.8730074 50.22483151 412.6226898
2019    3   17  5   56  0   0   108.9895097 50.53642185 412.7368197
2019    3   17  5   56  5   0   109.1078294 50.8478274  412.850563
2019    3   17  5   56  10  0   109.2280101 51.15904424 412.9640113
2019    3   17  5   56  15  0   109.3500969 51.47006828 413.0772319
2019    3   17  5   56  20  0   109.4741362 51.78089533 413.1901358
2019    3   17  5   56  25  0   109.6001758 52.09152105 413.3025291
2019    3   17  5   56  30  0   109.728265  52.40194099 413.414457
2019    3   17  5   56  35  0   109.8584548 52.71215052 413.5259984
2019    3   17  5   56  40  0   109.9907976 53.02214489 413.6371791

I desire to open this text file to create three arrays called lat[N], long[N], and time[N] where N is the number of rows in the file. I ultimately want to be able to determine what the latitude, longitude, and time is at any point. For example, lat[0] should return 50.22483151 if working properly. In addition, for the time, I would need to convert to decimal hours and then create the array.

Essentially I need help with opening this text file I have and then creating the three arrays.

I've tried this method for opening the file, but I get stuck when trying to write the array and I think I may not be opening the file correctly.

import numpy as np
 
file_name = 'C:\\Users\\Saman\\OneDrive\\Documents\\Orbit 1.txt'

data = []
with open(file_name) as file:
    next(file)
    for line in file:
        row = line.split()
        row = [float(x) for x in row]
        data.append(row)
4
  • Consider using pandas.read_table(). Commented Aug 10, 2020 at 19:02
  • @DYZ that isn't working for me. It is giving me this output Year\tMonth\tDay\tHour\tMinute\tSecond\tMillisecond\tLongitude\tLatitude\tAltitude 0 2019\t3\t17\t5\t55\t55\t0\t108.8730074\t50.224... 1 2019\t3\t17\t5\t56\t0\t0\t108.9895097\t50.5364... 2 2019\t3\t17\t5\t56\t5\t0\t109.1078294\t50.8478... 3 2019\t3\t17\t5\t56\t10\t0\t109.2280101\t51.159... 4 2019\t3\t17\t5\t56\t15\t0\t109.3500969\t51.470... ... ... 1111 rows × 1 columns Commented Aug 10, 2020 at 19:29
  • I don't understand why it is putting t in front of everything after the year Commented Aug 10, 2020 at 19:31
  • Have you read the function documentation? You have to pass the option sep=r"\s+". Commented Aug 10, 2020 at 19:33

2 Answers 2

1

The most effortless way to solve your problem is to use Pandas:

import pandas as pd
df = pd.read_table('Orbit 1.txt', sep=r'\s+')
df['Longitude']
#0    108.873007
#1    108.989510
#2    109.107829
#3    109.228010
#4    109.350097
#5    109.474136
#6    109.600176
#7    109.728265
#8    109.858455
#9    109.990798

Once you get a Pandas DataFrame, you may want to use it for the rest of the data processing, too.

Sign up to request clarification or add additional context in comments.

2 Comments

This was so helpful for the latitude and longitude, so thank you! I'm trying to convert the time to decimal hours now and this is the code I'm trying to use but I keep getting the error 'invalid literal for int() with base 10: 'Hour''. time = df[int('Hour') + int('Minute')/60 + int('Second')/3600 + int('Millisecond')/3600000] Do you have any recommendations to convert the time to decimal hours without getting the error?
df['Hour']+df['Minute']/60+...
0
file_name = 'info.txt'

Lat=[]
Long=[]
Time=[]
left_justified=lambda x: x+" "*(19-len(x))
right_justified=lambda x: " "*(19-len(x))+x

with open(file_name) as file:
    next(file)
    for line in file:
        data=line.split()
        Lat.append(data[8])
        Long.append(data[7])
        hrs=int(data[3])
        minutes=int(data[4])
        secs=int(data[5])
        total_secs=secs+minutes*60+hrs*3600
        Time.append(total_secs/3600)
        
print(left_justified("Time"),left_justified("Lat"),left_justified("Long"))
for i in range(len(Lat)):
    print(left_justified(str(Time[i])),left_justified(Lat[i]),left_justified(Long[i]))

Try this

1 Comment

So I've had this issue before and I'm still not sure what is going on. I am getting this error. IndexError Traceback (most recent call last) <ipython-input-13-6bbe41fb8aeb> in <module> 15 data=line.split() 16 print(data) ---> 17 Lat.append(data[8]) 18 Long.append(data[7]) 19 hrs=int(data[3]) IndexError: list index out of range

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.