Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

STRING TO LIST

 

 

First page Previous page 1 2 Next page Last page  View All Python python RSS feed   Index | Next | Previous | View Threaded


clp2 at rebertia

Jun 14, 2012, 1:06 AM

Post #26 of 26 (93 views)
Permalink
Re: string to list [In reply to]

On Thu, Jun 14, 2012 at 12:40 AM, Hemanth H.M <hemanth.hm [at] gmail> wrote:
>>>> list(literal_eval('"aa","bb 'b'","cc"'))
> ['aa', 'bb ', 'cc']
>
> Strange?

Not really. You didn't properly escape the embedded quotation marks in
the string itself!
So before anything ever even gets passed to literal_eval(), that part
is parsed as two adjacent literals: '"aa","bb ' and b'","cc"'
In Python 3.x, the "b" prefix indicates a `bytes` literal rather than
a `str` literal.

Implicit adjacent string literal concatenation then occurs.
Thus:
>>> print '"aa","bb ' b'","cc"'
"aa","bb ","cc"
Compare:
>>> print '''"aa","bb 'b'","cc"'''
"aa","bb 'b'","cc"

But really, literal_eval() should not be used for CSV; it won't handle
unquoted fields at all, among other issues.

Cheers,
Chris
--
http://mail.python.org/mailman/listinfo/python-list

First page Previous page 1 2 Next page Last page  View All Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.