470,636 Members | 1,487 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,636 developers. It's quick & easy.

Unicode string in exec()

1.I read a unicode file by using codec
2.I want to pass that string to exec() statement
3.But one of my character (U+0950) in that string is not showing
properly in the output got by that exec() statement

could anyone help me to get proper output. ?
somesh

Jul 19 '05 #1
3 3660
First off, I just have to correct your terminology. "exec" is a
statement, and doesn't require parentheses, so talking about "exec()"
invites confusion.

I'll answer your question in terms of eval(), which takes a string
representing a Python expression, interprets it, and returns the result.

In Python 2.3, the following works right:
eval(u"u'\u0190'") u'\u0190'
Here, the string passed to eval() contains the literal LATIN CAPITAL
LETTER OPEN E, and the expected unicode string is returned

The following behaves "surprisingly": eval(u"'\u0190'")

'\xc6\x90'
.... you seem to get the UTF-8 encoding of the unicode.

This is related to PEP 263 (http://www.python.org/peps/pep-0263.html)
but the behavior of compile(), eval() and exec don't seem to be spelled
out.

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFCn0c0Jd01MZaTXX0RAmSCAJ4ww3vaX76vtyCPJfbPk1 t/3rQZKgCcCND/
lnsVr6GvioPRmfo83gi+y6Y=
=m3n/
-----END PGP SIGNATURE-----

Jul 19 '05 #2
See below.
--------------

"Jeff Epler" <je****@unpythonic.net> wrote in message
news:ma**************************************@pyth on.org...

First off, I just have to correct your terminology. "exec" is a
statement, and doesn't require parentheses, so talking about "exec()"
invites confusion.

I'll answer your question in terms of eval(), which takes a string
representing a Python expression, interprets it, and returns the result.

In Python 2.3, the following works right:
eval(u"u'\u0190'") u'\u0190'
Here, the string passed to eval() contains the literal LATIN CAPITAL
LETTER OPEN E, and the expected Unicode string is returned

The following behaves "surprisingly": eval(u"'\u0190'")

'\xc6\x90'
.... you seem to get the UTF-8 encoding of the Unicode.

This is related to PEP 263 (http://www.python.org/peps/pep-0263.html)
but the behavior of compile(), eval() and exec don't seem to be spelled
out.

Jeff

[response]

To expand on Jeff's reply:

in the first example, he's passing a Unicode string to eval(),
which contains a Unicode string that contains a Unicode escape.
The result is a Unicode string containing a single Unicode character.

In the second example,
he's passing a Unicode string to eval(), which string contains
a ***normal*** string that contains a Unicode escape. The
Unicode escape produces two characters. The result is a
***normal*** string that contains two characters.

Is this your problem?

John Roth

Jul 19 '05 #3
I came out with proper solution today !
thanx for your response

Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Michael Weir | last post: by
8 posts views Thread by Bill Eldridge | last post: by
1 post views Thread by Mike Brown | last post: by
2 posts views Thread by Neil Schemenauer | last post: by
1 post views Thread by PRiya | last post: by
3 posts views Thread by andrew | last post: by
3 posts views Thread by Thomas Heller | last post: by
1 post views Thread by Korara | last post: by
???
1 post views Thread by Stoney L | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.