pythonpython-3.xdictionarytextfile-read

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


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.


Solution

  • 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.9\n']
        values = line.removeprefix('BBM ').split(' ')
        res[f'{values[0]} {values[1]}'] = values[2]