Splitting a quoted string.

mosscliffe mcl.office at googlemail.com
Wed May 16 08:38:38 EDT 2007


Thank you very much for all for your replies.

I am now much wiser to using regex and CSV.

As I am quite a newbie, I have had my 'class' education improved as
well.

Many thanks again

Richard

On May 16, 12:48 pm, Duncan Booth <duncan.bo... at invalid.invalid>
wrote:
> mosscliffe <mcl.off... 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