Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Python - Files and Dictinoary 1

Status
Not open for further replies.

shaharpan

Programmer
Mar 23, 2005
6
US
Read data from a text file and assign the data value to a python dictinoary.

#!C:/Python23/python.exe
for line in open('c:\\Python23\\abc.txt','r'):
line=line.strip()
if not line:
continue
m,n=line.split()
print m

this code work fine but the value of m and n should be assign to a dictinory

['item1':100,'item2':200]

how can be this made possible

let me know

 
Assuming your lines look like this:

item1 100
item2 200
....

before the loop declare:
mydict = {}

and after "m,n = line.split()" add:
mydict[m] = n

 
Thanks,

One more doubt,

How to Write a file using Shelves where the shelves contains a dictonary. and the dic contains the item and their key.

and what other operation can be performed on shelves,

If u can find a tutorial on Shelves, that would be great

thanz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top