I found a few other posts regarding this topic, but I'm having issues getting it to work for my instance; I am relatively new to Python so I apologize. Below is an example of the first few lines of a txt file that I have:
Year Month Day Hour Minute Second Millisecond Longitude Latitude Altitude
2019 3 16 22 0 0 0 -143.9558774 0.105859373 399.9938343
2019 3 16 22 0 5 0 -143.9204788 0.427070185 399.9951097
2019 3 16 22 0 10 0 -143.8850757 0.748280246 399.9977697
2019 3 16 22 0 15 0 -143.8496643 1.069488992 400.0018341
Every value is separated by a space and I want to create keys for each so it would be Year, Month, Day, Minute, Second, Millisecond, Longitude, Latitude, and Altitude.
Below is code I am attempting to use, but it's not working properly and throwing the following error below my code.
import numpy as np
from csv import DictReader
# string holding path to satellite orbit data file
path = 'Path'
orbit_data = {} #initialize dictionary
file = DictReader(open(path + 'orbit.txt','r')) #open input data file
for row in file:
for column, value in row.items():
orbit_data.setdefault(column, []).append(value)
for key in orbit_data:
if ((key=='Object') or (key=='Directory')): orbit_data[key]=np.array(orbit_data[key],dtype=str)
elif ((key=='Year') or (key=='Month') or (key=='Day') or (key=='Hour') or (key=='Minute') or (key=='Second')): orbit_data[key]=np.array(orbit_data[key],dtype=int)
else: orbit_data[key] = np.array(orbit_data[key],dtype=float)
ValueError Traceback (most recent call last)
<ipython-input-6-3afe156299a7> in <module>
13 if ((key=='Object') or (key=='Directory')): orbit_data[key]=np.array(orbit_data[key],dtype=str)
14 elif ((key=='Year') or (key=='Month') or (key=='Day') or (key=='Hour') or (key=='Minute') or (key=='Second')): orbit_data[key]=np.array(orbit_data[key],dtype=int)
---> 15 else: orbit_data[key] = np.array(orbit_data[key],dtype=float)
ValueError: could not convert string to float: '2019\t3\t16\t22\t0\t0\t0\t-143.9558774\t0.105859373\t399.9938343'
If you could please provide some guidance as to what I am doing wrong and how to fix it I would appreciate it!