What's the best way to extract 2 values from a CSV file from each row systematically?
Tim Chase
python.list at tim.thechases.com
Mon Sep 23 13:47:11 EDT 2013
On 2013-09-23 10:10, quarantinemiles at gmail.com wrote:
> 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
[snip]
> 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.
The csv module has several tools that make this easy to do. If there
are column-headers, you can do
import csv
with file("myfile.csv", "rb") as f:
for row in csv.DictReader(f)
insert_fields_into_form(
row["KGD"],
row["DME"],
)
which I like for clarity, ease of updating, and robustness (if for
some reason, the columns get moved around, or columns get
added/removed, as long as the headers remain the same, you can get
the data).
If it doesn't have headers, then you'd have to manually pick out the
columns, either by tuple-unpacking:
with file("myfile.csv", "rb") as f:
for _, _, kgd, _, dme in csv.reader(f)
insert_fields_into_form(kgd, dme)
or by directly indexing:
KGD_COL = 3
DME_COL = 5
with file("myfile.csv", "rb") as f:
for row in csv.reader(f)
insert_fields_into_form(
row[KGD_COL],
row[DME_COL],
)
both of which are more fragile than DictReader when it comes to
columns being added/removed. I leave the implementation of
insert_fields_into_form() up to you :-)
-tkc
More information about the Python-list
mailing list