How to read a text file and get the data in the middle of it into a python dictionary?

Question:

Among the data in a text file as below, date as key and decimal value (1.85 etc) as data should be taken into a python dictionary.

textfile(mytext.txt)

BBM 17/12/2023 15:15:04 1.85 2700.0 41857.9                                                                         
BBM 17/12/2023 16:00:02 1.68 2698.0 41992.8                                                                 
BBM 17/12/2023 16:45:04 1.6 2702.0 41908.3                                                         
BBM 17/12/2023 17:30:10 1.47 2706.0 41975.1                                                                                                    
BBM 17/12/2023 18:15:02 1.35 2692.0 41934.5                                                                                         

After reading above text file, my dictionary should be like this.

myDict = {
    '17/12/2023 15:15:04': 1.85,
    '17/12/2023 16:00:02': 1.68,
    '17/12/2023 16:45:04': 1.6,
    '17/12/2023 17:30:10': 1.47,
    '17/12/2023 18:15:02': 1.35
}

I tried several methods but none of them worked.

Asked By: Chaminda Kumara

||

Answers:

I think you can do it like this:

myDict = {}

with open('mytext.txt', 'r') as file:
    for line in file:
        words = line.split()
        date_time = ' '.join([words[1], words[2]])
        myDict[date_time] = float(words[3])

print(myDict)

I can’t try the file reading right now, but the algorithm itself worked for me when I just put the text into a variable directly.

Answered By: Ada

My solution:

res = {}

with open('./mytext.txt', 'r') as f:
    data = f.readlines()

# filter rows
for line in data:
    # remove prefix 'BBM '
    # split, take the date with hour (pos 0 and 1) and take the second value
    # example:
    # BBM 17/12/2023 15:15:04 1.85 2700.0 41857.9
    # after split: ['17/12/2023', '15:15:04', '1.85', '2700.0', '41857.9n']
    values = line.removeprefix('BBM ').split(' ')
    res[f'{values[0]} {values[1]}'] = values[2]
Answered By: AndreVale69

You can use a regex to parse that format:

import re 
pat=re.compile(r'^BBMs+(dd/dd/dddd dd:dd:dd)s+(d+.d+)')
with open(fn, 'r') as f:    
        your_dic={m.group(1):float(m.group(2)) 
            for line in f 
                if (m:=pat.search(line))}

>>> your_dic
{'17/12/2023 15:15:04': 1.85, '17/12/2023 16:00:02': 1.68, '17/12/2023 16:45:04': 1.6, '17/12/2023 17:30:10': 1.47, '17/12/2023 18:15:02': 1.35}
Answered By: dawg