By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,818 Members | 1,313 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,818 IT Pros & Developers. It's quick & easy.

Convert file with information into dictionary

P: 3
Hello, I have a text file with information on products, the first 3 lines can be seen below
CODBO, Call of Duty BlackOps, 120
SC2WL, Starcraft 2 WoL, 90
FONV0, Fallout New Vegas 100
CODBO is the ID, Call... is the name and 120 is the price.
What would the code be for a function that would turn that into a dictionary that looks similarly to this:
{'CODBO':('Call of Duty BlackOps',120), ...}
I have tried to think of a solution for 2 hours now and still dont have the faintest idea on how to do it. Any help is appreciated, thanks.

EDIT:
Thanks bvdet, i modified the code a bit and got it to work, here it is for reference.
Expand|Select|Wrap|Line Numbers
  1. def load_games(filename) :
  2.     games = {}
  3.     f = open(filename, 'U')
  4.     s = ''
  5.     for line in f:
  6.         s = line
  7.         sList = s.split(',')
  8.         games[sList[0]] = sList[1].strip(), int(sList[2]) 
  9.     return games
  10.  
  11. x = load_games('games.txt')
  12. print x
Apr 5 '11 #1

✓ answered by bvdet

Do you know how to open a file, read the file or iterate on the file object, and close the file? The following code will parse one of the lines and enter the information into a dictionary.
Expand|Select|Wrap|Line Numbers
  1. >>> dd = {}
  2. >>> s = "CODBO,  Call of Duty BlackOps,      120"
  3. >>> sList = s.split(",")
  4. >>> dd[sList[0]] = sList[1].strip(), int(sList[2])
  5. >>> dd
  6. {'CODBO': ('Call of Duty BlackOps', 120)}
  7. >>> 

Share this Question
Share on Google+
4 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Do you know how to open a file, read the file or iterate on the file object, and close the file? The following code will parse one of the lines and enter the information into a dictionary.
Expand|Select|Wrap|Line Numbers
  1. >>> dd = {}
  2. >>> s = "CODBO,  Call of Duty BlackOps,      120"
  3. >>> sList = s.split(",")
  4. >>> dd[sList[0]] = sList[1].strip(), int(sList[2])
  5. >>> dd
  6. {'CODBO': ('Call of Duty BlackOps', 120)}
  7. >>> 
Apr 5 '11 #2

P: 3
Yeh I know most of the basics with dictionaries and files, that code helps a lot, Ill try to include it in a for loop and iterate over every line. Ill post the code soon if it doesn't work. Thanks
Apr 6 '11 #3

P: 3
So far I can create a list with the games (code below), how can I convert it to a dictionary in the style {'CODBO':('Call of Duty BlackOps',120), ...}?
Expand|Select|Wrap|Line Numbers
  1. def load_games(filename) :
  2.     games = {}
  3.     f = open(filename, 'U')
  4.     s = ''
  5.     for line in f:
  6.         s += line
  7.     sList = s.split(',')
  8.     return sList
  9.  
  10. x = load_games('games.txt')
  11. print x
Has the output
Expand|Select|Wrap|Line Numbers
  1. >>> 
  2. ['CODBO', ' Call of Duty BlackOps', ' 120\nSC2WL', ' Starcraft 2 WoL', ' 90\nFONV0', ' Fallout New Vegas', ' 100']
  3. >>> 
Apr 6 '11 #4

bvdet
Expert Mod 2.5K+
P: 2,851
Like this (untested):
Expand|Select|Wrap|Line Numbers
  1. def load_games(filename) :
  2.     games = {}
  3.     f = open(filename)
  4.     for line in f:
  5.         sList = s.strip().split(",")
  6.         games[sList[0]] = sList[1].strip(), int(sList[2])
  7.     f.close()
  8.     return sList
  9.  
  10. x = load_games('games.txt')
  11. print x
Apr 7 '11 #5

Post your reply

Sign in to post your reply or Sign up for a free account.