What's the best way to extract 2 values from a CSV file from each row systematically?

Joel Goldstick joel.goldstick at gmail.com
Mon Sep 23 13:25:51 EDT 2013


On Mon, Sep 23, 2013 at 1:10 PM, <quarantinemiles at gmail.com> wrote:

> Hey guys,
>
> I'm a little new to Python, and am still learning!
>
> I'm test building a web scraper that extracts prices from a website, based
> on two values I want to extract from a CSV file. The CSV has at least 1000
> rows, an example:
>
> 0,0,KGD,0,DME,0,,0,0
>
> The values I want to extract are KGD and DME (columns 3 and 5).
>
> Each row in the CSV file contains values in columns 3 and 5 that I'd like
> to extract. What's the best way to extract these data, so I can insert them
> as inputs in two different fields in a form? A list, dictionary, or MySQL?
> I try not to do anything with MySQL as I'm not familiar with it at all.
>
> I'm thinking of dictionary because at least I can make it work as a
> key/value pair (for columns 3 and 5), but a dictionary is unordered. I'd
> like to automatically go through each row in the CSV file from beginning to
> end to extract the two values in columns 3 and 5 and insert them into
> fields in a form.
>
> I'd really appreciate any suggestions or help, thanks in advance!
>
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>

You should check out the csv module here:
http://docs.python.org/2/library/csv.html#module-csv
It will read your csv file into a list (the file rows) of lists (the
columns).  You can easily loop over the data to extract the columns you
want using list indexing

-- 
Joel Goldstick
http://joelgoldstick.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130923/68c920bf/attachment.html>


More information about the Python-list mailing list