Hi,
I have a CSV file from excel that looks like this (simplified):
name,description,type1,type2,name,filename
test1,this is a test,0.000,1.000,,
test2,another test,1.000,0.000,newname,filename
test3,this is a test,0.000,1.000,,
test4,this is a test,0.000,1.000,,
test5,this is a test,0.000,1.000,,
test6,this is a test,0.000,1.000,,
what i want at the end is a dictionary like
dict1[name] = test1
dict1[description] = "this is a test"
..
..
..
dict2[name] = test2
dict2[description] = "another test"
whats a fast and quick way of parsing it? Look for the first comma,
strip that entry out of the string, keep going? Thought there might be
some shortcuts.
Thanks 8 19795
Hank wrote: Hi,
I have a CSV file from excel that looks like this (simplified):
name,description,type1,type2,name,filename test1,this is a test,0.000,1.000,, test2,another test,1.000,0.000,newname,filename test3,this is a test,0.000,1.000,, test4,this is a test,0.000,1.000,, test5,this is a test,0.000,1.000,, test6,this is a test,0.000,1.000,,
what i want at the end is a dictionary like
dict1[name] = test1 dict1[description] = "this is a test" . . . dict2[name] = test2 dict2[description] = "another test"
import csv
dicts = []
inputFile = open("SomeDurnFileName.csv", "rb")
parser = csv.reader(inputFile)
firstRec = True
for fields in parser:
if firstRec:
fieldNames = fields
firstRec = False
else:
dicts.append({})
for i,f in enumerate(fields)
dicts[-1][fieldNames[i]] = f
<ac*****@easystreet.com> schrieb im Newsbeitrag
news:3F***************@easystreet.com... Hank wrote: Hi,
I have a CSV file from excel that looks like this (simplified):
..........
import csv
dicts = []
inputFile = open("SomeDurnFileName.csv", "rb") parser = csv.reader(inputFile)
If you do not use Python 2.3 for some reason or other there is a third party
CSV that is fast and workes fine with Python 2.2 (but uses a different
interface of course) http://www.object-craft.com.au/projects/csv/
Kindly
Michael P
>>>>> "Hank" == Hank <so*********@yahoo.ca> writes:
Hank> Hi, I have a CSV file from excel that looks like this
Hank> (simplified):
Hank> name,description,type1,type2,name,filename test1,this is a
Hank> test,0.000,1.000,, test2,another
Hank> test,1.000,0.000,newname,filename test3,this is a
Hank> test,0.000,1.000,, test4,this is a test,0.000,1.000,,
Hank> test5,this is a test,0.000,1.000,, test6,this is a
Hank> test,0.000,1.000,,
I posted some code recently to parse CSV into a dictionary where you
can extract rows or columns by key or index number. It's not exactly
what you asked for, but might help http://groups.google.com/groups?hl=e...hon.org&rnum=4
John Hunter
where would i get this csv module from? does it come with python 2.2?
thanks ac*****@easystreet.com wrote in message news:<3F***************@easystreet.com>... Hank wrote: Hi,
I have a CSV file from excel that looks like this (simplified):
name,description,type1,type2,name,filename test1,this is a test,0.000,1.000,, test2,another test,1.000,0.000,newname,filename test3,this is a test,0.000,1.000,, test4,this is a test,0.000,1.000,, test5,this is a test,0.000,1.000,, test6,this is a test,0.000,1.000,,
what i want at the end is a dictionary like
dict1[name] = test1 dict1[description] = "this is a test" . . . dict2[name] = test2 dict2[description] = "another test"
import csv
dicts = []
inputFile = open("SomeDurnFileName.csv", "rb") parser = csv.reader(inputFile) firstRec = True for fields in parser: if firstRec: fieldNames = fields firstRec = False else: dicts.append({}) for i,f in enumerate(fields) dicts[-1][fieldNames[i]] = f
"Hank" <so*********@yahoo.ca> schrieb im Newsbeitrag
news:73**************************@posting.google.c om... where would i get this csv module from? does it come with python 2.2?
thanks
No of course - read my posting!
Kindly
Michael P where would i get this csv module from? does it come with python 2.2? thanks
It comes with Python 2.3. If you grab the csv and _csv modules from the
Python 2.3 CVS repository, they should build and run under 2.2.x. If that's
not possible, poke around for Object Craft's csv module.
Skip
soundwave56> I have a CSV file from excel that looks like this
soundwave56> (simplified):
soundwave56> name,description,type1,type2,name,filename
soundwave56> test1,this is a test,0.000,1.000,,
soundwave56> test2,another test,1.000,0.000,newname,filename
soundwave56> test3,this is a test,0.000,1.000,,
soundwave56> test4,this is a test,0.000,1.000,,
soundwave56> test5,this is a test,0.000,1.000,,
soundwave56> test6,this is a test,0.000,1.000,,
Take a look at the csv module delivered with Python 2.3.
Skip
I'm using ActivePython2.2 which has the win32 extensions included.
Don't think they have a ActivePython2.3 out yet.
Also I was trying out PAGE which needed 2.2. Don't have the resources
to recompile for 2.3.
Thanks for the help!
"Michael Peuser" <mp*****@web.de> wrote in message news:<bj*************@news.t-online.com>... <ac*****@easystreet.com> schrieb im Newsbeitrag news:3F***************@easystreet.com... Hank wrote: Hi,
I have a CSV file from excel that looks like this (simplified):
.........
import csv
dicts = []
inputFile = open("SomeDurnFileName.csv", "rb") parser = csv.reader(inputFile)
If you do not use Python 2.3 for some reason or other there is a third party CSV that is fast and workes fine with Python 2.2 (but uses a different interface of course) http://www.object-craft.com.au/projects/csv/
Kindly Michael P This discussion thread is closed Replies have been disabled for this discussion. Similar topics
reply
views
Thread by XspiriX |
last post: by
|
3 posts
views
Thread by david |
last post: by
|
7 posts
views
Thread by M |
last post: by
|
3 posts
views
Thread by yehaimanish |
last post: by
| | | |
reply
views
Thread by =?Utf-8?B?Q2xhcmU=?= |
last post: by
|
31 posts
views
Thread by broli |
last post: by
| | | | | | | | | | |