Need suggestion to speed up code...

Emile van Sebille emile at fenx.com
Thu May 10 22:36:50 EDT 2001


Except for the embedded tics ('), you could eval the string to get a tuple
of values, and perhaps some appropriate pre/post processing would take care
of those.

>>> a = "(1, 'foo', 'bar', 34, 3.14159, 'an imbedded comma, this sting has',
'this one isn''t so easy either')"
>>> b = a.replace("''","`")
>>> eval(b)
(1, 'foo', 'bar', 34, 3.1415899999999999, 'an imbedded comma, this sting
has', 'this one isn`t so easy either')

HTH

--

Emile van Sebille
emile at fenx.com

---------
"Roy Smith" <roy at panix.com> wrote in message
news:roy-54AC09.22222810052001 at news1.panix.com...
> I need to split up a string into a list of fields.  The strings are value
> lists from SQL statements, and look something like this:
>
> (1, 'foo', 'bar', 34, 3.14159, 'an imbedded comma, this sting has', 'this
> one isn''t so easy either')
>
> If it wasn't for the fact that I need to handle commas and quotes imbedded
> in quoted strings, it would be trivial -- just a call to string.split.
> But, as it is, the best I can figure out is to walk the string, character
> by character, keeping track of what state I'm in (parsing an integer,
> parsing a floating point, or parsing a quoted string).  It works, but
> profiling shows it's the bottleneck in my whole program.
>
> Anybody have any idea for a better way to do this?
>
> If I really had to, I suppose I could write this bit in C as an extension
> module, but I'd rather do a pure python implementation to keep it
portable.





More information about the Python-list mailing list