Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
python compare and process a csv file
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Chris Rebert  
View profile  
 More options Nov 4, 7:49 pm
Newsgroups: comp.lang.python
From: Chris Rebert <c...@rebertia.com>
Date: Wed, 4 Nov 2009 00:49:31 -0800
Local: Wed, Nov 4 2009 7:49 pm
Subject: Re: python compare and process a csv file
On Tue, Nov 3, 2009 at 7:43 AM, Siva Subramanian

Have you considered using a proper SQL database? (See
http://en.wikipedia.org/wiki/SQL ; MySQL is one example:
http://en.wikipedia.org/wiki/MySQL)
Mucking around with CSV files like this is basically doing the work of
some simple SQL queries, only in an ad-hoc, inefficient manner. (MS
Access is essentially a non-industrial-strength SQL for
non-programmers.)

Cheers,
Chris
--
http://blog.rebertia.com


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Otten  
View profile  
 More options Nov 4, 9:22 pm
Newsgroups: comp.lang.python
Followup-To: comp.lang.python
From: Peter Otten <__pete...@web.de>
Date: Wed, 04 Nov 2009 11:22:04 +0100
Local: Wed, Nov 4 2009 9:22 pm
Subject: Re: python compare and process a csv file

Industrial strength or not, Access should be capable of solving the OP's
problem. So it would be interesting what's so bad about it in this case.

Anyway, here's a database-free python solution:

import csv

REPORT = "report.csv"
CUSTOMERS = "customers.csv"

with open(CUSTOMERS) as instream:
    next(instream) # skip header

    # put customer ids into a set for fast lookup
    customer_ids = set(line.strip() for line in instream)

with open(REPORT) as instream:
    rows = csv.reader(instream)

    # find columns
    headers = [column.strip() for column in rows.next()]
    customer_column = headers.index("Customer ID")
    sum_over_columns = [headers.index(s) for s in "stat1 vol2
amount3".split()]

    # initialize totals
    sigma = [0] * len(headers)

    # calculate totals
    for row in rows:
        if row[customer_column] in customer_ids:
            for index in sum_over_columns:
                sigma[index] += int(row[index])
# print totals
for index in sum_over_columns:
    print "%-10s %10d" % (headers[index] + ":", sigma[index])

The limiting factor for this approach is the customer_ids set which at some
point may no longer fit into memory.

Peter


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google