简体   繁体   English

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

[英]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.我对 Python 比较陌生,所以我很抱歉。 Below is an example of the first few lines of a txt file that I have:下面是我拥有的 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

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!如果您能提供一些关于我做错了什么以及如何解决它的指导,我将不胜感激!

You could using pandas.to_dict("list") as follows:您可以使用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)

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

The default delimiter for any CSV reader is a comma.任何 CSV 阅读器的默认分隔符是逗号。 You didn't change that.你没有改变那个。 As a result, you read the entire line as a single value.结果,您将整行读取为单个值。 You have one key, that being the entire header line.你有一把钥匙,那就是整个 header 线。 You then set the value to the entire data line.然后将该值设置为整个数据行。 This causes your error.这会导致您的错误。

Create your reader properly:正确创建您的阅读器:

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

Make sure that you strip the line as well.确保你也strip了这条线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM