473,471 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Create 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 1449
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_items(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
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...
354
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...
8
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...
1
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)...
2
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...
11
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?...
2
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 ...
1
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...
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
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...
1
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
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,...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.