Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > help with data extraction

Reply
Thread Tools

help with data extraction

 
 
Amitava Maity
Guest
Posts: n/a
 
      02-10-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
 
Reply With Quote
 
 
 
 
Andrew Seaford
Guest
Posts: n/a
 
      02-10-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="(E-Mail Removed)">(E-Mail Removed)</a>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
data extraction, and analysis of text files mambenanje@gmail.com Java 6 07-07-2006 03:54 AM
Data extraction Bob nospam Computer Support 5 06-13-2004 09:33 PM
Perl Patten Matching and Data Extraction Manasa Perl 2 05-19-2004 12:16 PM
Data extraction from MS Word file madhukp ASP .Net 1 02-09-2004 09:36 PM
Clock and data extraction Di'ego VHDL 0 12-21-2003 02:22 AM



Advertisments