help with data extraction
Andrew Seaford
andrew at simx.co.uk
Sat Feb 9 22:25:11 EST 2008
Amitava Maity wrote:
> 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,
>
The code does not skip the 1st row with 'cond' and 'section'. The file
is read correctly and the data is appended to the wind and temp lists.
Resulting in the two lists below
wind = [0,30,45,0]
temp = [4,4,32,64]
The line conditions = dict(zip(wind, temp)) creates a dictionary using
the wind and temp lists. In a dictionary each key is unique. The first
key is 0 which is assigned the value 4. The second key is 30 with the
value 4. The third key is 45 with the value 32. The key 0 is assigned a
new value of 64. The result is that the dictionary has three key value
pairs, not four.
Andrew Seaford
Simulation Director
Simulation eXpertise
web <a href="http://www.simx.co.uk">www.simx.co.uk</a>
email <a href="mailto:andrew at simx.co.uk">andrew at simx.co.uk</a>
More information about the Python-list
mailing list