Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > string split

Reply
Thread Tools

string split

 
 
Leland
Guest
Posts: n/a
 
      01-09-2009
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

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

Thanks,
Leland
 
Reply With Quote
 
 
 
 
Jerry Hill
Guest
Posts: n/a
 
      01-09-2009
On Fri, Jan 9, 2009 at 3:39 PM, Benjamin Kaplan
> This looks like a CSV file to me. If that is the case, it is easier to use
> the built-in csv module than to try to write your own parser.


It should be as easy as this:

import csv

testfile = open('testfile.csv', 'w')
testdata = """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",
"""
testfile.write(testdata)
testfile.close()

infile = open('testfile.csv', 'r')
reader = csv.reader(infile)
for line in reader:
print line


The output of that code is:

['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', '']

so line[0] is your part number, etc.

--
Jerry
 
Reply With Quote
 
 
 
 
Leland
Guest
Posts: n/a
 
      01-09-2009
On Jan 9, 12:57*pm, "Jerry Hill" <(E-Mail Removed)> wrote:
> On Fri, Jan 9, 2009 at 3:39 PM, Benjamin Kaplan
>
> > This looks like a CSV file to me. If that is the case, it is easier to use
> > the built-in csv module than to try to write your own parser.

>
> It should be as easy as this:
>
> import csv
>
> testfile = open('testfile.csv', 'w')
> testdata = """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",
> """
> testfile.write(testdata)
> testfile.close()
>
> infile = open('testfile.csv', 'r')
> reader = csv.reader(infile)
> for line in reader:
> * * print line
>
> The output of that code is:
>
> ['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', '']
>
> so line[0] is your part number, etc.
>
> --
> Jerry


It works like a charm.

Really appreciate all the helps.
 
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
How to use String.split to split a mixed encoding string(partencoded in gbk, part encoded in utf-8) Stanley Xu Ruby 2 03-23-2011 02:06 PM
a split is not a split Dumbell Computer Support 3 03-09-2009 10:45 PM
String#split(/\s+/) vs. String#split(/(\s+)/) Sam Kong Ruby 5 08-12-2006 07:59 PM
How can I split database results with ExecuteReader and Split? needin4mation@gmail.com ASP .Net 2 05-05-2006 10:36 PM
Small inconsistency between string.split and "".split Carlos Ribeiro Python 11 09-17-2004 05:57 PM



Advertisments