473,385 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Convert file with information into dictionary

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. >>> 

4 7036
bvdet
2,851 Expert Mod 2GB
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
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
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
2,851 Expert Mod 2GB
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

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

Similar topics

0
by: Crystal | last post by:
Hi there, I need to retrieve multi files information. Instead of creating my own file brower, I think I can use Windows Explore to navigate these files. Can someone tell me how I can get the file...
2
by: Craig | last post by:
I've seen many examples of how to call SHGetFileInfo in shell32.dll to get a files associated icon, but I can't find anywhere how to get the file information (size, last date modified, etc, etc)...
1
by: sonu | last post by:
how to retrive client side file information through .NET Any link or information please ?
1
by: Locke Nash Cole | last post by:
Can someone point me in the right direction? I'm looking to get extended file information on .exe or .dlls such as File Version Description Copyright Language Internal Name Product Name...
1
by: Alan T | last post by:
I want to convert file type another such as convert from HTML to .txt convert from Excel to .txt convert from Word to .txt convert from PPT to .txt so that I can run it programmatically in...
3
by: TREVOR SILKSTONE | last post by:
I need to CONVERT file.ASP TO HTM or HTML
1
LeoTD
by: LeoTD | last post by:
Dear all, I want to convert file type .doc to .html in Linux. But I can't be get it, please show me How to coding for convert it ? Thanks a lot.
10
by: deciacco | last post by:
I'm writing a command line utility to move some files. I'm dealing with thousands of files and I was wondering if anyone had any suggestions. This is what I have currently: $arrayVirtualFile =...
1
by: deepthisoft | last post by:
Hi, I am creating site statistics tool.For that I have stored the log file information into Sql server database through ODBC Option in IIS. I got date, time, ip, port,target,status,error status...
1
by: gaurav92K | last post by:
sir, is it possible that convert file system fat, fat32 to ntfs in usb drive.give the best answer. gaurav92k.
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.