Home > Net >  Trying to add data to different dictionaries and print one then another
Trying to add data to different dictionaries and print one then another

Time:04-08

I am trying to create two different dictionaries with my code, then, print them separately. I've been messing with the update() function but nothing is working, here is my code.

 def readTempData(filename):
    with open(filename, 'r') as f:
        for line in f.readlines():
            day, high, low = line.split(',')
            high = int(high)
            low = int(low)
            day = int(day)
            highs = dict.update({'Day: {}, High: {}'}.format(day, high))
            lows = dict.update({'Day: {}, Low: {}'}.format(day, low))
        print(highs)
        print(lows)

readTempData(input("Enter file name:"))

AttributeError: 'set' object has no attribute 'format'

Thank you in advance for any help!

CodePudding user response:

A few different problems here. First, what is your dict named (dict isn't instantiated anywhere that I can see)? You don't define it as far as I can see. As for update, the parameter is not a string and there's no reason to use format; the keys in the object can be strings, which is likely why you're confused, but those commas aren't wrapped in a string. I also don't see why you're using update here rather than just setting the properties values in general.

Finally, as with the other comments, I'm also not clear on what you want the dict to look like after, or why you'd separate highs and lows into separate dictionaries.

I'd probably do this:

def readTempData(filename):
    with open(filename, 'r') as f:
        data = {}
        for line in f.readlines():
            day, high, low = line.split(',')
            high = int(high)
            low = int(low)
            day = int(day)
            data[day] = {'high': high, 'low': low}
        print(data)

readTempData(input("Enter file name:"))


You then can see the day as the key, with the high and low on the day easily accessible. If you want a seperate dict of high / low, you should clarify what you want as a key. You could also use a list of dictionaries which contain the day, high, and low.

For example, to access the high temperature on day 3:

data[3]['high']

CodePudding user response:

I think you're muddling together the tasks of formatting/printing and updating a dictionary. This might be closer to what you're trying to do:

def readTempData(filename):
    highs = {}
    lows = {}
    with open(filename, 'r') as f:
        for line in f.readlines():
            day, high, low = map(int, line.split(','))
            highs[day] = high
            lows[day] = low
    return highs, lows

highs, lows = readTempData(input("Enter file name:"))
for day, high in highs.items():
    print(f"Day: {day}, High: {high}")
for day, low in lows.items():
    print(f"Day: {day}, Low: {low}")

Here the readTempData function returns two dictionaries, highs and lows, each of which is keyed on the day -- after we call that function, we iterate through each dictionary and print out the data that it contains, using the formatting that you were trying to apply in your original code.

FWIW, I think it would be easier to work with this data if it were in a single dictionary, or maybe just a list, but that depends to some extent on what you want to do with this data other than print it.

  • Related