在Python中从文本文件创建字典



我发现了一些关于这个主题的其他帖子,但我有问题,让它为我的实例工作;我对Python比较陌生,所以我很抱歉。下面是我的txt文件的前几行示例:

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

每个值由一个空格分隔,我想为每个值创建键,所以它将是年,月,日,分钟,秒,毫秒,经度,纬度和海拔。

下面是我试图使用的代码,但它不能正常工作,并在我的代码下面抛出以下错误。

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'

如果你能提供一些指导,我做错了什么,以及如何解决它,我将不胜感激!

您可以这样使用pandas.to_dict("list"):

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)

结果:

{'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]}

任何CSV阅读器的默认分隔符都是逗号。你没有改变这一点。因此,将整行读取为单个值。您有一个键,这是整个标题行。然后将值设置为整个数据线。这将导致错误。

创建合适的阅读器:

file = DictReader(open('orbit.txt','r'), delimiter=' ')  #open input data file

确保你也用strip

最新更新