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. - def load_games(filename) :
-
games = {}
-
f = open(filename, 'U')
-
s = ''
-
for line in f:
-
s = line
-
sList = s.split(',')
-
games[sList[0]] = sList[1].strip(), int(sList[2])
-
return games
-
-
x = load_games('games.txt')
-
print x
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. - >>> dd = {}
-
>>> s = "CODBO, Call of Duty BlackOps, 120"
-
>>> sList = s.split(",")
-
>>> dd[sList[0]] = sList[1].strip(), int(sList[2])
-
>>> dd
-
{'CODBO': ('Call of Duty BlackOps', 120)}
-
>>>
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. - >>> dd = {}
-
>>> s = "CODBO, Call of Duty BlackOps, 120"
-
>>> sList = s.split(",")
-
>>> dd[sList[0]] = sList[1].strip(), int(sList[2])
-
>>> dd
-
{'CODBO': ('Call of Duty BlackOps', 120)}
-
>>>
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
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), ...}? - def load_games(filename) :
-
games = {}
-
f = open(filename, 'U')
-
s = ''
-
for line in f:
-
s += line
-
sList = s.split(',')
-
return sList
-
-
x = load_games('games.txt')
-
print x
Has the output - >>>
-
['CODBO', ' Call of Duty BlackOps', ' 120\nSC2WL', ' Starcraft 2 WoL', ' 90\nFONV0', ' Fallout New Vegas', ' 100']
-
>>>
bvdet 2,851
Expert Mod 2GB
Like this (untested): - def load_games(filename) :
-
games = {}
-
f = open(filename)
-
for line in f:
-
sList = s.strip().split(",")
-
games[sList[0]] = sList[1].strip(), int(sList[2])
-
f.close()
-
return sList
-
-
x = load_games('games.txt')
-
print x
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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)...
|
by: sonu |
last post by:
how to retrive client side file information through .NET
Any link or information please ?
|
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...
|
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...
| |
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.
|
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 =...
|
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...
|
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.
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
| |