0

the idea is to write a python code reading data from a text file indicated below.

https://www.minorplanetcenter.net/iau/MPCORB/CometEls.txt

Further on, I would like to filter comet-data like name magnitude etc. etc.

Right now my problem is getting data output.

My code is:

import os.path

word_dict = {}

scriptpath = os.path.dirname(__file__)
filename = os.path.join(scriptpath, 'CometEls.txt','r')

for line in filename:
    line = line.strip()
    relation = line.split(' ')
    word_dict[relation[0]] = relation[0:20]


    while True:
        word = input('Comet name : ')
        if word in word_dict:
        print ('Comets in list :' , word_dict[word])
        print(filename) #show file location
    else:
        print( 'No comet data!')
        print(word_dict) #show data from dictionary

As you can see data in my dictionary isn't the comet-data.

enter image description here

It should be

enter image description here

Typing in "a"

enter image description here

Theoretically the code works, the problem is creating the dictionary?

Maybe I'm completely wrong, but it doesn't work neither with tuples or lists, or it's better to copy data into a .csv file?

Best regards

Robert
  • 67
  • 1
  • 9

1 Answers1

0

You saved the file path to the filename variable, but did not open the file for reading. You should open file, and then read it:

import os.path

word_dict = {}

scriptpath = os.path.dirname(__file__)
file_path = os.path.join(scriptpath, 'CometEls.txt')

with open(file_path, 'r') as commet_file:
    for line in commet_file:
        line = line.strip()
        relation = line.split(' ')
        word_dict[relation[0]] = relation[0:20]

while True:
    word = input('Comet name : ')
    if word in word_dict:
        print ('Comets in list :' , word_dict[word])
        print(file_path) 
    else:
        print('No comet data!')
        print(word_dict) #show data from dictionary

Also, your example has wrong margins near in while block, please check it.

Mikhail Sidorov
  • 1,325
  • 11
  • 15