Splitting a quoted string.
Duncan Booth
duncan.booth at invalid.invalid
Wed May 16 07:48:44 EDT 2007
mosscliffe <mcl.office at googlemail.com> wrote:
> I am looking for a simple split function to create a list of entries
> from a string which contains quoted elements. Like in 'google'
> search.
>
> eg string = 'bob john "johnny cash" 234 june'
>
> and I want to have a list of ['bob', 'john, 'johnny cash', '234',
> 'june']
>
> I wondered about using the csv routines, but I thought I would ask the
> experts first.
>
> There maybe a simple function, but as yet I have not found it.
You probably need to specify the problem more completely. e.g. Can the
quoted parts of the strings contain quote marks? If so how what are the
rules for escaping them. Do two spaces between a word mean an empty field
or still a single string delimiter.
Once you've worked that out you can either use re.split with a suitable
regular expression, or use the csv module specifying your desired dialect:
>>> class mosscliffe(csv.Dialect):
delimiter = ' '
quotechar = '"'
doublequote = False
skipinitialspace = False
lineterminator = '\r\n'
quoting = csv.QUOTE_MINIMAL
>>> csv.register_dialect("mosscliffe", mosscliffe)
>>> string = 'bob john "johnny cash" 234 june'
>>> for row in csv.reader([string], dialect="mosscliffe"):
print row
['bob', 'john', 'johnny cash', '234', 'june']
More information about the Python-list
mailing list