Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: split string at commas respecting quotes when string not in csvformat

Reply
Thread Tools

Re: split string at commas respecting quotes when string not in csvformat

 
 
Terry Reedy
Guest
Posts: n/a
 
      03-26-2009
R. David Murray wrote:
> OK, I've got a little problem that I'd like to ask the assembled minds
> for help with. I can write code to parse this, but I'm thinking it may
> be possible to do it with regexes. My regex foo isn't that good, so if
> anyone is willing to help (or offer an alternate parsing suggestion)
> I would be greatful. (This has to be stdlib only, by the way, I
> can't introduce any new modules into the application so pyparsing is
> not an option.)
>
> The challenge is to turn a string like this:
>
> a=1,b="0234,)#($)@", k="7"
>
> into this:
>
> [("a", "1"), ("b", "0234,)#($)#"), ("k", "7")]


But the starting string IS is csv format, where the values are strings
with the format name=string.

>>> import csv
>>> myDialect = csv.excel
>>> myDialect.skipinitialspace = True # needed for space before 'k'
>>> a=list(csv.reader(['''a=1,b="0234,)#($)@", k="7"'''], myDialect))[0]
>>> a

['a=1', 'b="0234', ')#($)@"', 'k="7"']
>>> b=[tuple(s.split('=',1)) for s in a]
>>> b

[('a', '1'), ('b', '"0234'), (')#($)@"',), ('k', '"7"')]

Terry Jan Reedy

 
Reply With Quote
 
 
 
 
John Machin
Guest
Posts: n/a
 
      03-26-2009
On Mar 27, 8:43*am, Terry Reedy <(E-Mail Removed)> wrote:
> R. David Murray wrote:
> > OK, I've got a little problem that I'd like to ask the assembled minds
> > for help with. *I can write code to parse this, but I'm thinking it may
> > be possible to do it with regexes. *My regex foo isn't that good, so if
> > anyone is willing to help (or offer an alternate parsing suggestion)
> > I would be greatful. *(This has to be stdlib only, by the way, I
> > can't introduce any new modules into the application so pyparsing is
> > not an option.)

>
> > The challenge is to turn a string like this:

>
> > * * a=1,b="0234,)#($)@", k="7"

>
> > into this:

>
> > * * [("a", "1"), ("b", "0234,)#($)#"), ("k", "7")]

>
> But the starting string IS is csv format, where the values are strings
> with the format name=string.
>
> *>>> import csv
> *>>> myDialect = csv.excel
> *>>> myDialect.skipinitialspace = True # needed for space before 'k'
> *>>> a=list(csv.reader(['''a=1,b="0234,)#($)@", k="7"'''], myDialect))[0]
> *>>> a
> ['a=1', 'b="0234', ')#($)@"', 'k="7"']
> *>>> b=[tuple(s.split('=',1)) for s in a]
> *>>> b
> [('a', '1'), ('b', '"0234'), (')#($)@"',), ('k', '"7"')]
>


It's in the csv format that Excel accepts on input but this is
irrelevant. The output does not meet the OP's requirements; it has
taken the should-have-been-protected comma as a delimiter, and
produced FOUR elements instead of THREE ... also note '"0234' has a
leading " and ')#($)@"' has a trailing "

 
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
split string at commas respecting quotes when string not in csv format R. David Murray Python 8 03-27-2009 02:19 PM
How to split a string containing nested commas-separated substrings Robert Dodier Python 5 06-25-2008 06:30 AM
replace double inverted commas into slash double inverted commas in string AviraM Java 2 09-28-2006 06:19 PM
split a line, respecting double quotes Jim Python 8 07-10-2006 03:55 PM
Perl expression for parsing CSV (ignoring parsing commas when in double quotes) GIMME Perl 2 02-11-2004 05:40 PM



Advertisments