string split

Discussion in 'Python' started by Leland, Jan 9, 2009.

  1. Leland

    Leland Guest

    Hi,

    I have some formatted strings that I'd like to split and get the
    meaningful data, here is the example of the string format. The big
    difference of these two line are the second double quote set at the
    second line
    100-01001-001,"Diode,Small Signal,SOT-23",1,D46,
    100-01004-001,"Diode,High Voltage General Purpose,600mA,200V,SOT-23",
    3,"D24,D72,D104",

    I want to split the string into the following format:
    '100-01001-001', 'Diode,Small Signal,SOT-23', '1', 'D46'
    '100-01004-001', 'Diode,High Voltage General Purpose,600mA,
    200V,SOT-23', '3', 'D24,D72,D104'

    By doing so, the list then has the meaning of part #, description,
    quantity and reference. Here is the way I way to split:
    str2=line1.split(',\"', 1) # Split part# and the rest
    key = str2[0]
    str3 = str2[1]
    str4 = str3.split("\",", 1)
    Value1 = str4[0]
    str5 = str4[1]
    str6 = str5.split(",", 1) # QTY, PARTS & BOM_NOTES
    Quanty = str6[0]
    str7 = str6[1] # PARTS & BOM_NOTES
    if str7[0] == "\"" :
    str8=str7.split("\"", 2)
    str9=str8[1]
    else :
    str8=str7.split(",", 1)
    str9=str8[0]
    print(key, ":", str9)

    It seems work this way, but is there more elegant way to do this?

    Thanks,
    Leland
    Leland, Jan 9, 2009
    #1
    1. Advertising

  2. On Fri, 09 Jan 2009 12:39:22 -0800, Leland wrote:

    > It seems work this way, but is there more elegant way to do this?


    Yes, the `csv` module in the standard library.

    Ciao,
    Marc 'BlackJack' Rintsch
    Marc 'BlackJack' Rintsch, Jan 9, 2009
    #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. Replies:
    2
    Views:
    445
  2. Carlos Ribeiro
    Replies:
    11
    Views:
    677
    Alex Martelli
    Sep 17, 2004
  3. trans.  (T. Onoma)

    split on '' (and another for split -1)

    trans. (T. Onoma), Dec 27, 2004, in forum: Ruby
    Replies:
    10
    Views:
    194
    Florian Gross
    Dec 28, 2004
  4. Sam Kong
    Replies:
    5
    Views:
    215
    Rick DeNatale
    Aug 12, 2006
  5. Stanley Xu
    Replies:
    2
    Views:
    572
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page