help with data extraction

Discussion in 'Python' started by Amitava Maity, Feb 10, 2008.

  1. 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
    Amitava Maity, Feb 10, 2008
    #1
    1. Advertising

  2. 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:"></a>
    Andrew Seaford, Feb 10, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Di'ego

    Clock and data extraction

    Di'ego, Dec 21, 2003, in forum: VHDL
    Replies:
    0
    Views:
    451
    Di'ego
    Dec 21, 2003
  2. Manasa
    Replies:
    2
    Views:
    766
    Mothra
    May 19, 2004
  3. madhukp

    Data extraction from MS Word file

    madhukp, Feb 9, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    487
    Steve C. Orr [MVP, MCSD]
    Feb 9, 2004
  4. Replies:
    6
    Views:
    512
    Carlos Eduardo Lima Borges
    Jul 7, 2006
  5. Dave Kuhlman

    HTML data extraction?

    Dave Kuhlman, Dec 22, 2003, in forum: Python
    Replies:
    2
    Views:
    369
    John J. Lee
    Dec 22, 2003
Loading...

Share This Page