MagicToaster
MagicToaster

Reputation: 127

Python: Append to dictionary in for loop

I'm getting unexpected results when trying to populate a dict with output from the Linux fio command.

with open('normal_output.txt', 'rb') as f:
    for line in f.readlines():
        d = {}

        if ':' not in line:
            continue
        print("LINE: ", line)
        key, value = line.strip().split(':', 1)
        d[key] = value

I expect a dictionary full of KVPs like:

('KEY: ', 'READ')
('VALUE: ', ' io=131220KB, aggrb=13890KB/s, minb=13890KB/s, maxb=13890KB/s, mint=9447msec, maxt=9447msec')
('KEY: ', 'WRITE')
('VALUE: ', ' io=130924KB, aggrb=13858KB/s, minb=13858KB/s, maxb=13858KB/s, mint=9447msec, maxt=9447msec')
('KEY: ', 'Disk stats (read/write)')
('VALUE: ', '')
('KEY: ', 'sda')
('VALUE: ', ' ios=32642/32600, merge=0/12, ticks=7472/936, in_queue=8392, util=88.18%')
{'sda': ' ios=32642/32600, merge=0/12, ticks=7472/936, in_queue=8392, util=88.18%'}

But instead, I'm just getting the very last KVP:

{'sda': ' ios=32642/32600, merge=0/12, ticks=7472/936, in_queue=8392, util=88.18%'}

I tried d[key].append(value) but that gives me: KeyError: 'fio_test'

Upvotes: 1

Views: 2185

Answers (2)

chasmani
chasmani

Reputation: 2510

You are instantiating the dictionary in the for loop, so every time it loops you are overwriting your dictionary. Bring d={} outside the for loop

Upvotes: 3

Ignacio Vergara Kausel
Ignacio Vergara Kausel

Reputation: 6026

In your loop you're creating a new empty dictionary d. Move it outside the for loop.

From

with open('normal_output.txt', 'rb') as f:
    for line in f.readlines():
        d = {}
        # do work

to

with open('normal_output.txt', 'rb') as f:
    d = {}
    for line in f.readlines():
        # do work

You can even move the dictionary creation outside the context manager (with statement) to keep the logic better organized, but that's more of a matter of taste and style.

Upvotes: 5

Related Questions