help with data extraction

Amitava Maity maity.amitava at gmail.com
Sat Feb 9 21:17:22 EST 2008


Hello,

I have a data file (data.csv) that is something like this:

data, Conductor, ACSR
data, diameter, 0.02862
data, cross-section, 0.0004845
data, weight, 1.621
data, Mod, 7000000000
data, Uts, 13450
data, Coef, 0.0000193
data, Cr, 20
data, span, 350
data, Wind pres, 0
data, temp, 32
data, ten, 3326
cond, Final wind press, 0, Final temp, 4
cond, Final wind press, 30, Final temp, 4
cond, Final wind press, 45, Final temp, 32
cond, Final wind press, 0, Final temp, 64
section, 234, 267, 289, 197

I need to to extract the third element from the rows with 'data' as
the first element, the third and fifth element from the rows with
'cond' as the first element and
all the elements following the 'section' element in the rows with
'section' as the first element.

here is the code used to extract the data:

import csv

keys = ["type", "d", "x", "c", "m", "u", "a", "tcr", "s", "wi", "ti", "Ti"]
values = []
wind   = []
temp   = []
sec    = []


reader = csv.reader(open("data.csv", "rb"))

for row in reader:
    if row[0] == 'data': values.append(row[2])
    if row[0] == 'cond': wind.append(row[2]), temp.append(row[4])
    if row[0] == 'section': sec = row[1:]


inputs = dict(zip(keys, values))
conditions = dict(zip(wind, temp))
condition = tuple(conditions.items())

print inputs, condition, sec

What I can't understand here is why the 1st row with 'cond' data and
1st element with 'section' data being skipped.

What is the fix?

thanks in advance,

-- 
amaity



More information about the Python-list mailing list