Problem in splitting a string

Peter Otten __peter__ at web.de
Thu Jul 22 14:57:10 EDT 2004


Angelo Secchi wrote:

> 
> Hi,
> I have string of numbers and words like
> 
> ',,,,,,23,,,asd,,,,,"name,surname",,,,,,,\n'
> 
> and I would like to split (I'm using string.split()) it using comma as
> separator but I do not want to split in two also the "name,surname"
> field. In other word I would like python in separating fields to skip
> that particular comma.
> 
> How can I do that?

A minimal example using the csv module:

import csv
import cStringIO as stringio

stream = stringio.StringIO(
"""\
,,,,,,23,,,asd,,,,,"name,surname",,,,,,,
,,,,,,23,,,asd,,,,,"jo,black",,,,,,,
,,,,,,23,,,asd,,,,,"will,do",,,,,,,
""")
# could be:
# stream = file("tmp.csv")
for record in csv.reader(stream):
    print record

Peter




More information about the Python-list mailing list