我发现了一些关于这个主题的其他帖子,但我在让它为我的实例工作时遇到了问题;我对 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: '2019\t3\t16\t22\t0\t0\t0\t-143.9558774\t0.105859373\t399.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]}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句