473,804 Members | 2,143 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

quick and smart way for parsing a CSV file?

Hi,

I have a CSV file from excel that looks like this (simplified):

name,descriptio n,type1,type2,n ame,filename
test1,this is a test,0.000,1.00 0,,
test2,another test,1.000,0.00 0,newname,filen ame
test3,this is a test,0.000,1.00 0,,
test4,this is a test,0.000,1.00 0,,
test5,this is a test,0.000,1.00 0,,
test6,this is a test,0.000,1.00 0,,

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 19879
Hank wrote:

Hi,

I have a CSV file from excel that looks like this (simplified):

name,descriptio n,type1,type2,n ame,filename
test1,this is a test,0.000,1.00 0,,
test2,another test,1.000,0.00 0,newname,filen ame
test3,this is a test,0.000,1.00 0,,
test4,this is a test,0.000,1.00 0,,
test5,this is a test,0.000,1.00 0,,
test6,this is a test,0.000,1.00 0,,

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("SomeDurnF ileName.csv", "rb")
parser = csv.reader(inpu tFile)
firstRec = True
for fields in parser:
if firstRec:
fieldNames = fields
firstRec = False
else:
dicts.append({} )
for i,f in enumerate(field s)
dicts[-1][fieldNames[i]] = f
Jul 18 '05 #2

<ac*****@easyst reet.com> schrieb im Newsbeitrag
news:3F******** *******@easystr eet.com...
Hank wrote:

Hi,

I have a CSV file from excel that looks like this (simplified):
..........

import csv

dicts = []

inputFile = open("SomeDurnF ileName.csv", "rb")
parser = csv.reader(inpu tFile)


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*********@ya hoo.ca> writes:

Hank> Hi, I have a CSV file from excel that looks like this
Hank> (simplified):

Hank> name,descriptio n,type1,type2,n ame,filename test1,this is a
Hank> test,0.000,1.00 0,, test2,another
Hank> test,1.000,0.00 0,newname,filen ame test3,this is a
Hank> test,0.000,1.00 0,, test4,this is a test,0.000,1.00 0,,
Hank> test5,this is a test,0.000,1.00 0,, test6,this is a
Hank> test,0.000,1.00 0,,

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*****@easystr eet.com wrote in message news:<3F******* ********@easyst reet.com>...
Hank wrote:

Hi,

I have a CSV file from excel that looks like this (simplified):

name,descriptio n,type1,type2,n ame,filename
test1,this is a test,0.000,1.00 0,,
test2,another test,1.000,0.00 0,newname,filen ame
test3,this is a test,0.000,1.00 0,,
test4,this is a test,0.000,1.00 0,,
test5,this is a test,0.000,1.00 0,,
test6,this is a test,0.000,1.00 0,,

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("SomeDurnF ileName.csv", "rb")
parser = csv.reader(inpu tFile)
firstRec = True
for fields in parser:
if firstRec:
fieldNames = fields
firstRec = False
else:
dicts.append({} )
for i,f in enumerate(field s)
dicts[-1][fieldNames[i]] = f

Jul 18 '05 #5

"Hank" <so*********@ya hoo.ca> schrieb im Newsbeitrag
news:73******** *************** ***@posting.goo gle.com...
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,descriptio n,type1,type2,n ame,filename
soundwave56> test1,this is a test,0.000,1.00 0,,
soundwave56> test2,another test,1.000,0.00 0,newname,filen ame
soundwave56> test3,this is a test,0.000,1.00 0,,
soundwave56> test4,this is a test,0.000,1.00 0,,
soundwave56> test5,this is a test,0.000,1.00 0,,
soundwave56> test6,this is a test,0.000,1.00 0,,

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*****@easyst reet.com> schrieb im Newsbeitrag
news:3F******** *******@easystr eet.com...
Hank wrote:

Hi,

I have a CSV file from excel that looks like this (simplified):

.........


import csv

dicts = []

inputFile = open("SomeDurnF ileName.csv", "rb")
parser = csv.reader(inpu tFile)


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
2489
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): #!/usr/bin/env python import os, sys nargs = len(sys.argv)
3
12113
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
2340
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 data records up and return them to an application. The complexity here is that I won't know the exact format of the files until run time. The files may or may not contain headers and trailers
3
4234
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 array and manipulate each row by foreach(...) and insert/update in the database accordingly. If the file is small, it works well. If the file is large (say > 5mb), it generates memory allocation error. However I came to know that the allowed...
2
1693
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 # mysql_connect('127.0.0.1:3306', 'user', 'pass') or die('Could not connect: ' . mysql_error()); mysql_select_db('database'); #
8
3749
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 12 1 15 2 13 3 6 3 9 I need to read the above file and have the following information in the output file In APPLE 0 occurs 1 time, 1 occurs 3 times, 2 occurs 1 time, 3 occurs 3 times
4
1947
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. #include <xercesc/dom/deprecated/DOMParser.hpp> #include <xercesc/util/PlatformUtils.hpp> #include <xercesc/dom/DOM_Document.hpp> #include <xercesc/dom/deprecated/DOM_DOMException.hpp> #include...
0
2005
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 builder, but the problem is the same. How to sovle the problem? Regards, Clare
31
2524
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 chunks. Can any one please explain what is all this about ?
0
10353
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10356
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10099
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9176
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7643
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 presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4314
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3836
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3003
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.