473,785 Members | 2,851 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

High level csv reader

It occured to me that most times I read a csv file, I'm often doing
from scratch things like assigning labels to columns, mapping fields to
the appropriate type, ignoring some fields, changing their order, etc.
Before I go on and reinvent the wheel, is there a generic high level
wrapper around csv.reader that does all this ?

Thanks,
George

Nov 1 '06 #1
6 1462
George Sakkis wrote:
It occured to me that most times I read a csv file, I'm often doing
from scratch things like assigning labels to columns, mapping fields to
the appropriate type, ignoring some fields, changing their order, etc.
Before I go on and reinvent the wheel, is there a generic high level
wrapper around csv.reader that does all this ?

Thanks,
George
There is a csv in the standard library. Though many of us don't mind
answering questions like this, you can get a lot of answers quicker by
(1) looking to see what's in the standard library and (2) using google.

http://docs.python.org/lib/module-csv.html

James
Nov 1 '06 #2
James Stroud wrote:
>Before I go on and reinvent the wheel, is there a generic high level
wrapper around csv.reader that does all this ?

There is a csv in the standard library.
I'm not sure the "csv" module qualifies as a high-level wrapper around
itself, though.

</F>

Nov 1 '06 #3

James Stroud wrote:
George Sakkis wrote:
It occured to me that most times I read a csv file, I'm often doing
from scratch things like assigning labels to columns, mapping fields to
the appropriate type, ignoring some fields, changing their order, etc.
Before I go on and reinvent the wheel, is there a generic high level
wrapper around csv.reader that does all this ?

Thanks,
George

There is a csv in the standard library. Though many of us don't mind
answering questions like this, you can get a lot of answers quicker by
(1) looking to see what's in the standard library and (2) using google.

http://docs.python.org/lib/module-csv.html

James
The OP mentioned "csv.reader ". This indicates to me that he *has* read
the csv docs (have you?), and is *already* using the csv module. The
tasks he says he does often are *not* covered by the standard library.
He appears to be asking if there is a higher-level wrapper around the
standard library.

Please consider reading his question carefully.

Nov 1 '06 #4
George Sakkis wrote:
It occured to me that most times I read a csv file, I'm often doing
from scratch things like assigning labels to columns, mapping fields to
the appropriate type, ignoring some fields, changing their order, etc.
Before I go on and reinvent the wheel, is there a generic high level
wrapper around csv.reader that does all this ?
Hi George,

Firstly one thing to remember: if you are going to reinvent the wheel,
don't forget to also reinvent the axle :-)

AFAIK there is no such animal.

I would need a lot of persuasion that a wrapper or toolbox wasn't just
overhead with little benefit. For example, ignoring some fields and/or
changing the order of others can be done rather simply:

| >>inrow
| ['a', 'b', 'c', 'd', 'e', 'f']
Suppose we want to ignore the "vowels" and reverse the order of the
others:
| >>outrow = list(inrow[k] for k in (5, 3, 2, 1))
| >>outrow
| ['f', 'd', 'c', 'b']

I don't see the value in creating (and documenting!) a one-line
function

def gather_list_ite ms(input_list, item_map):
return list(input_list[k] for k in item_map)

NB something like this is already in the mx kit somewhere IIRC.

Cheers,
John

Nov 1 '06 #5

GeorgeIt occured to me that most times I read a csv file, I'm often
Georgedoing from scratch things like assigning labels to columns,
Georgemapping fields to the appropriate type, ignoring some fields,
Georgechanging their order, etc. Before I go on and reinvent the
Georgewheel, is there a generic high level wrapper around csv.reader
Georgethat does all this ?

I'm not aware of anything that specifically addresses these ideas. Here are
some thoughts though:

* If you use the DictReader class you can ignore fields you aren't
interested in more easily since you access the fields of interest by
name and with the fieldnames parameter to the constructor can assign
column names to csv data which lacks it (use similar functionality in
DictWriter to create column labels on output). You can also specify
the restkey parameter to the constructor and thus only specify the
fields of interest in the fieldnames parameter. (I think. I've never
actually used that capability, but that's what the documentation
suggests.)

* There was a thread earlier this month with this subject:

paseline(my favorite simple script): does something similar exist?

Check it out for a number of different solutions to formatting the
fields in a line of text. The idea can easily be extended to a list
or dict of values instead, perhaps in a subclass of DictReader.

Skip
Nov 1 '06 #6
sk**@pobox.com wrote:
GeorgeIt occured to me that most times I read a csv file, I'm often
Georgedoing from scratch things like assigning labels to columns,
Georgemapping fields to the appropriate type, ignoring some fields,
Georgechanging their order, etc. Before I go on and reinvent the
Georgewheel, is there a generic high level wrapper around csv.reader
Georgethat does all this ?

I'm not aware of anything that specifically addresses these ideas. Here are
some thoughts though:

* If you use the DictReader class you can ignore fields you aren't
interested in more easily since you access the fields of interest by
name and with the fieldnames parameter to the constructor can assign
column names to csv data which lacks it (use similar functionality in
DictWriter to create column labels on output). You can also specify
the restkey parameter to the constructor and thus only specify the
fields of interest in the fieldnames parameter. (I think. I've never
actually used that capability, but that's what the documentation
suggests.)

* There was a thread earlier this month with this subject:

paseline(my favorite simple script): does something similar exist?

Check it out for a number of different solutions to formatting the
fields in a line of text. The idea can easily be extended to a list
or dict of values instead, perhaps in a subclass of DictReader.

Skip
Indeed, they are both relevant; actually Fredrik's suggestion in that
thread was my starting point. Here's my my current API for the (most
typical) case of fixed-size rows, addressing my most common
requirement, field conversions (no named columns for now):
http://rafb.net/paste/results/4UgvSD50.html.

It looks somewhat involved but it's more along the lines of "making
easy things easy and hard things possible". Comments and suggestions
are most welcome.

George

Nov 1 '06 #7

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

Similar topics

75
6193
by: Howard Nease | last post by:
Hello, everyone. I would appreciate any advice that someone could give me on my future career path. Here is my situation: I am a bright Junior in a very well-respected private high school, taking almost all AP and accelerated classes. I am HIGHLY interested in technology, more specifically the field of Computer Science and software engineering. I have heard a whole lot about the fact that the market for software engineers nowadays is...
354
15927
by: Montrose... | last post by:
After working in c# for a year, the only conclusion I can come to is that I wish I knew c. All I need is Linux, the gnu c compiler and I can do anything. Web services are just open sockets hooked up to interfaces. The Gtk is more than enough gui.
8
2176
by: York | last post by:
Hi, R language has very high-level IO functions, its read.table can read a total .csv file and recogonize the types of each column. write.table can do the reverse. R's MySQL interface has high-level functions, too, e.g. dbWriteTable can automatically build a MySQL table and write a table of R data into it.
1
1457
by: Robert Ludewig | last post by:
Hello, I recently started to develop my own little high level, easy to use 3D API on top of managed directx for .net. The idea behind it is having a generic API that lets me create (simpler) 3D-applications in the shortest possible time in .net. It involves implementing things like a node structure for the scenegraph, a console, device configuration manager etc. Just like we know it from other APIs, like java3d or openinventor or from...
2
1336
by: Support | last post by:
Hello: I am trying to learn about Aactive Directory in VB.NET and NT security, so if you have any good resources/references, please let me know. At a high level ... in VB.NET If I am a member of a domain user group - what do I need to do in order to assign read only rights to a file I created, to that group (effectively shutting me out from being able to edit the file of course..)
11
1855
by: Gleb Rybkin | last post by:
I searched online, but couldn't really find a standard package for working with Python and XML -- everybody seems to suggest different ones. Is there a standard xml package for Python? Preferably high-level, fast and that can parse in-file, not in-memory since I have to deal with potentially MBs of data. Thanks.
2
1901
by: KUTTAN | last post by:
I a using System.Reflection System.Runtime.InteropServices System.Security.Permissions In my web application In this, Reflection need low thrust level to run But I have hosted my site in a shared server. So thrust level is high
1
3405
by: Flanders | last post by:
I have developed a small arcade game with the help of a few VB books. A scoring system was implemented in the design of the game but I as hoping that some one would be able to instruct me on how display the score at the end of the game. At the moment when a player finishes the game I have a window displaying "Puzzle Solved." If I could display the players score under this in the same window would be great. This is a quick copy and paste...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10329
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10152
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...
0
9950
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
8974
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 project—planning, coding, testing, and deployment—without 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...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4053
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
3650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2880
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.