Skip to content
Advertisement

Creating a Dictionary from a Text File in Python

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: '2019t3t16t22t0t0t0t-143.9558774t0.105859373t399.9938343'

If you could please provide some guidance as to what I am doing wrong and how to fix it I would appreciate it!

Advertisement

Answer

You could using pandas.to_dict("list") as follows:

import pandas as pd
if __name__ == '__main__':
    input_path = "data/orbit.txt"
    orbit_data = pd.read_csv(input_path, sep="s+", engine="python").to_dict("list")
    print(orbit_data)

Result:

{'Year': [2019, 2019, 2019, 2019], 'Month': [3, 3, 3, 3], 'Day': [16, 16, 16, 16], 'Hour': [22, 22, 22, 22], 'Minute': [0, 0, 0, 0], 'Second': [0, 5, 10, 15], 'Millisecond': [0, 0, 0, 0], 'Longitude': [-143.9558774, -143.9204788, -143.8850757, -143.84966430000003], 'Latitude': [0.105859373, 0.427070185, 0.748280246, 1.0694889920000001], 'Altitude': [399.99383430000006, 399.9951097, 399.9977697, 400.0018341]}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement