Search code examples
pythonfiledictionarysplitaverage

How to split the textfile


04-05-1993:1.068

04-12-1993:1.079

04-19-1993:1.079

06-06-1994:1.065

06-13-1994:1.073

06-20-1994:1.079


I have text file for date-year-price for gas and i want to calculate the avg gas prices for year. So i tried to split,

with open('c:/Gasprices.txt','r') as f: 
   fullfile=[x.strip() for x in f.readlines()]
datesprices=[(x.split('-')[0], x.split(':')[1]) for x in fullfile]
print(datesprices)

But I can't get year and price data but data like this.

('04', '1.068'), ('04', '1.079')

please let me know what should i know.

and plus, please let me know how to use split data to calculate the avg price per year using a dictionary if you can.


Solution

  • I see no need to split the input lines as they have a fixed format for the date - i.e., its length is known. Therefore we can just slice.

    with open('gas.txt') as gas:
        td = dict()
        for line in gas:
            year = line[6:10]
            price = float(line[11:])
            td.setdefault(year, []).append(price)
        for k, v in td.items():
            print(f'{k} {sum(v)/len(v):.3f}')
    

    Output:

    1993 1.075
    1994 1.072
    

    Note:

    There is no check here for blank lines. It is assumed that there are none and that the sample shown in the question is malformed.

    Also, no need to strip the incoming lines as float() is impervious to leading/trailing whitespace