string split

Leland lelandpeng at gmail.com
Fri Jan 9 16:05:24 EST 2009


On Jan 9, 12:57 pm, "Jerry Hill" <malaclyp... at gmail.com> 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.



More information about the Python-list mailing list