[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

help with data extraction

Amitava Maity

2/10/2008 2:17:00 AM

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
2 Answers

Andrew Seaford

2/10/2008 3:25:00 AM

0

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...
email <a href="mailto:andrew@simx.co.uk">andrew@simx.co.uk</a>

Gregg Hagglund

3/21/2008 5:10:00 AM

0

On Thu, 20 Mar 2008 18:36:32 -0400, Kim P <yduzitmatter@cogeco.ca>
wrote:

>feministe wrote:
>> "Nelson" <nhenderson3@cogeco.ca> a ?crit dans le message de
>> news:DorEj.30541$612.1808@read1.cgocable.net...
>>> Having better things to do than attack attackers of scientology every
>>> day, I shake the dust off my shoes and I leave this foul place to
>>> devour itself.
>>> Nelson
>>>
>>
>> Yeah, just come here, I'd bet that you've left the soil to-day since
>> you've been put in either power, either confusion. Due to the problems
>> of scientology, I'd bet that most staffs and OSA helpers are in
>> under-confusion. They know where they are, but don't know why they are.
>> This is underconfusion, yes.
>>
>> The formula is simple: leave the cult.
>>
>> r
>
>I have a feeling his remarks about Casey Hill are what is motivating
>this current departure. Those remarks could garner some serious
>problems for him....and scientology. That case caused considerable
>problems for scientology and its leaders in Canada and many lawyers
>watched it closely. Nelson can run but he cannot hide if the law
>decides this needs to be addressed.
>
>Also,IIRC - Nelson is a declared member trying desperately to get back
>into its good graces - not the way to do that NElson- posting the
>comments you did about a sitting judge - especially with the history of
>scientology in Canada - that criminal conviction and all (not to mention
>the largest libel suit in Canadian history).
>
>
>Kim P

I think Brian Mulroney bveat that. Didn't he get 30 million for the
RCMP defamation?

GH SP7