473,399 Members | 3,656 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,399 software developers and data experts.

quick and smart way for parsing a CSV file?

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
Jul 18 '05 #1
8 19863
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
Jul 18 '05 #2

<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

Jul 18 '05 #3
>>>>> "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

Jul 18 '05 #4
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

Jul 18 '05 #5

"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
Jul 18 '05 #6
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

Jul 18 '05 #7

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

Jul 18 '05 #8
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

Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: XspiriX | last post by:
Hi I am new here and so in python :-) I am trying to make a script for extracting (parsing?) some values from a file and write them into an output i thought to follow this example (cookbook): ...
3
by: david | last post by:
Is there a stream reader that can pasrse files which are in HTML format (i.e can't be passed as normal text files)? thanks David
7
by: M | last post by:
Hi, I need to parse text files to extract data records. The files will consist of a header, zero or more data records, and a trailer. I can discard the header and trailer but I must split the...
3
by: yehaimanish | last post by:
I am developing an application by which to parse the content from the access_log and insert it into the database. Since each row is an different entry, I am using file() to get the contents into an...
2
by: Robbo | last post by:
I have set up a php script to send a notification email to customers one week after initially placing their order. Here's the code: <? # First, open a database connection #...
8
by: shivam001 | last post by:
I have the following file as the input APPLE 0 118 1 110 1 125 1 135 2 110 3 107 3 115 3 126 ORANGE 0 112 1 119 2 109 2 119 3 112 4 109 4 128 MANGO 0 136 1 143 2 143 3 143 4 136 BANANA 0 5 1...
4
by: Arengin | last post by:
HI everyone, I am new with Xerces-C but I managed to get a DOMTree runing and to extracte the data to the screen. However I need to write the Tree back to a file.... and this is where I am stuck. ...
0
by: =?Utf-8?B?Q2xhcmU=?= | last post by:
Hi groups, VS2005 + Mobile SDK 6 work well on my PC, but after installing platform builder 5.0, VS2005 shows “XML parsing error” when open *.sln. We reinstall VS2005, Mobile SDK and platform...
31
by: broli | last post by:
I need to parse a file which has about 2000 lines and I'm getting told that reading the file in ascii would be a slower way to do it and so i need to resort to binary by reading it in large...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.