M
M
Mikhail Mikhail2018-06-09 15:46:05
Python
Mikhail Mikhail, 2018-06-09 15:46:05

How to correctly read data from a file in python of a different type?

Good afternoon. I have a small class for reading data from files. In this class, I read data from a file, it is presented in a .csv file in 2 lines in this way

метан, этан, пропан, изобутан, бутан, изопентан, пентан, изогексан, гексан, изогептан, гептан, изооктан, октан, изононан, нонан
0, 0, 0, 0, 0, 0, 0, 21, 34, 60, 105, 128, 50, 80, 42

and in the method itself I am trying to read the data and replace the names of the elements with numbers, I use a dictionary
here is the file itself
spoiler
class loading_flle:
    '''
    класс по считыванию файлов
    '''
    def __init__(self):
        self.paraphine = {
            'метан': -161.5,
            'этан': -88.6,
            'пропан': -42.1,
            'бутан': -0.5,
            'изобутан': -11.7,
            'пентан': 36.1,
            'изопентан': 27.9,
            'неопентан': 9.5,
            'гексан': 68.73,
            'изогексан': 60.3,
            'демитилбутан': 49.7,
            'изогептан': 90,
            'гептан': 98.4,
            'метилгексан': 90.1,
            'этилпентан': 93.5,
            'диментилпентан': 79.2,
            'триптан': 80.9,
            'октан': 125.68,
            'метилгептан': 117.6,
            'деметилгексан': 106.8,
            'метил-3-этилпентан': 115.7,
            'изооктан': 109.8,
            'тетраметилбутан': 106.5,
            'изононан': 143.3,
            'нонан': 150.8,
            'декан': 174.1,
            'ундекан': 195.9,
            'додекан': 216.3,
            'эйкозан': 342.7,
            'тридекан': 235.4,
            'тетрадекан': 250,
            'водород': 252.9
        }
        self.data = []
        self.load_data_on_files()
    def load_data_on_files(self):
        '''
        метод считывающий исходные данные из файлов
        '''
        for file_filename in os.listdir(directory_load_file_csv):
            x, y = np.genfromtxt(directory_load_file_csv +
                                 file_filename, dtype='str', delimiter=', ')
            print(x[0])
            # x,y = np.loadtxt(directory_load_file_csv +
            #   file_filename, delimiter=',')
            for i, item in enumerate(x):
                x[i] = self.paraphine[x[i]]
            if len(x) != len(y):  # проверка данных
                print('разное количество данных! перепроверьте!!!!!!!')
            self.data.append([x, y])


but it doesn't quite work :\ gives an
error
x = self.paraphine[x]
KeyError: ‘метан’

can you suggest how to fix it?
Edited by druidich92 (today 15:33:33)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ruslan., 2018-06-09
@LaRN

Perhaps the word 'methane' in the script and in the file is different, but this is not visible visually.
I mean that some of the letters, for example, are English.
Try replacing the word 'methane' in the script and in the file with some other word, such as the English version of 'methan', and see if the error occurs when you run the script.
And also, to make sure that everything works as intended, print the contents of the dictionary through print (self. paraphine) before executing the loop, something may be wrong with it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question