help with data extraction

A

Amitava Maity

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,
 
A

Andrew Seaford

Amitava said:
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:[email protected]">[email protected]</a>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top