简体   繁体   中英

Unable to update the output in python dictionary

I have the output in below format in test.csv file

/,9.8G,6.8G,27%
/home,4.8G,3.6G,22%
/opt,9.8G,5.2G,44%
/tmp,3.9G,3.6G,2%

I'm reading the input from test.csv file and converting into dictionary.

import csv
output1={}
output2={}
with open('test.csv', mode='r') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    for row in csv_reader:
        Mounted = row[0]
        Total = row[1]
        used = row [3]
        available = row[2]
        st = {}.fromkeys(['Mount'],Mounted)
        st1 = {}.fromkeys(['Total'],Total)
        st2 = {}.fromkeys(['used'],used)
        st3 = {}.fromkeys(['available'],available)
        st = dict(st.items() + st1.items() + st2.items() + st3.items())
        output1.update(st)
        print  'output1 = ',output1

output2["DiskStatus"] = output1
print output2

Receiving output in below format

output1 =  {'available': '6.8G', 'used': '27%', 'Mount': '/', 'Total': '9.8G'}
output1 =  {'available': '3.6G', 'used': '22%', 'Mount': '/home', 'Total': '4.8G'}
output1 =  {'available': '5.2G', 'used': '44%', 'Mount': '/opt', 'Total': '9.8G'}
output1 =  {'available': '3.6G', 'used': '2%', 'Mount': '/tmp', 'Total': '3.9G'}

{'DiskStatus': {'available': '19G', 'used': '1%', 'Mount': '/log', 'Total': '20G'}}

I'm expecting output in below format. Please help on this.

output1 =  {'available': '6.8G', 'used': '27%', 'Mount': '/', 'Total': '9.8G'},{'available': '3.6G', 'used': '22%', 'Mount': '/home', 'Total': '4.8G'},{'available': '5.2G', 'used': '44%', 'Mount': '/opt', 'Total': '9.8G'},{'available': '3.6G', 'used': '2%', 'Mount': '/tmp', 'Total': '3.9G'}

{'DiskStatus': [{'available': '19G', 'used': '1%', 'Mount': '/log', 'Total': '20G'},{'available': '3.6G', 'used': '22%', 'Mount': '/home', 'Total': '4.8G'},{'available': '5.2G', 'used': '44%', 'Mount': '/opt', 'Total': '9.8G'},{'available': '3.6G', 'used': '2%', 'Mount': '/tmp', 'Total': '3.9G'}]}

Looks like you need a list of dictionaries.

Ex:

import csv
disk_status = {'DiskStatus': []}
header = ['Mount', 'Total', 'available', 'used']
with open('test.csv') as infile:
    reader = csv.reader(infile)
    for line in reader:
        disk_status["DiskStatus"].append(dict(zip(header, line)))
print(disk_status)

Output:

{'DiskStatus': [{'Mount': '/',
                 'Total': '9.8G',
                 'available': '6.8G',
                 'used': '27%'},
                {'Mount': '/home',
                 'Total': '4.8G',
                 'available': '3.6G',
                 'used': '22%'},
                {'Mount': '/opt',
                 'Total': '9.8G',
                 'available': '5.2G',
                 'used': '44%'},
                {'Mount': '/tmp',
                 'Total': '3.9G',
                 'available': '3.6G',
                 'used': '2%'}]}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM