简体   繁体   中英

Read dictionary from a text file in a tabular form

I have successfully written a dictionary in a text file but i am not able to read it.

l={}
a=0
lo=eval(input("enter no of times"))
for i in range (lo):
    name=str(input('Enter name'))
    sal=eval(input('Enter sal'))
    no=eval(input('Enter no'))
    acsal=sal*no
    a=a+acsal
    l.update({name:acsal})
print(a)
print (l)
f=open("try.txt","w+")
f.write("BILL\n")
f.write(str(l))
f.write(a)
print (f.read())
f.close()

moreover i wanted the output to be shown in a tabular format for eg. if the file has a dictionary {"a":50,"b":60} i wanted it to be read as:

a  50
b  60

First of all, eval is not what you should use.

Convert to specific types instead.

Second, use json or jaml. Those are made by people way smarter than me or you.

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