472,358 Members | 1,935 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,358 software developers and data experts.

Python beginner, unicode encode/decode Q

1 Objective to write little programs to help me learn German. See code
after numbered comments. //Thanks in advance for any direction or
suggestions.

tk

2 Want keyboard answer input, for example:

answer_str = raw_input(' Enter answer ') Herr ▄Ř

[ I keyboard in the following characters Herr ▄Ř ]
print answer_str
Output on screen is Herr ▄Ř

3 history 1 and 2 code run interactively under Debian Linux Python
2.4 and interactively under windows98, first edition IDLE, Python 2.3.5
and it works.

4 history 3 and 4 code run from within a .py file produce different
output from example in book.

5 want to operate under Debian Linux but because the program failed
under Linux when I tried to run the code from a file in Linux Python, I
thougt I should fire up the win98 Idle/python program and try it to see
if ran there but it failed, too from within a file.

6 The sample code is from page 108-109 of: "Python for Dummies"
It says in the book: "Python's file objects and StringIO objects
don't support raw Unicode; the usual workaround is to encode Unicode as
UTF-8 before saving it to a file or stringIO object.
The sample code from the book is French as indicate here but trying
German produces the same result.

7 I have searched the net under all the keywords but this is as close as
I get to accomplishing my task. I suspect I may not be understanding:
StringIO objects don't support raw Unicode, but I don't know.
#_*_ coding: utf-8 _*_

# code run under linux debian interactively from a terminal and works

print " u'Libert\u00e9' "

# y = raw_input('Enter >') commented out

y = u'Lbert\u00e9'
y.encode('utf-8')
q = y.encode('utf-8')
q.decode('utf-8')
print q.decode('utf-8')

history 1 works and here is the screen copy of interactive
>>y = raw_input ('>')
Libert\xc3\xa9
>>q = 'Libert\xc3\xa9'
q.decode('utf-8')
u'Libert\xe9'
>>print q
LibertÚ
>>>
[ screen output is next line ]

LbertÚ

history 2
# code run under win98, first edition, within IDLE interactively and
succeeded in produce correct results.
# y = raw_input('Enter >') commented out

y = u'Lbert\u00e9'
y.encode('utf-8')
q = y.encode('utf-8')
q.decode('utf-8')
print q.decode('utf-8')

history 1 works and here is the screen copy of interactive
>>y = raw_input ('>')
Libert\xc3\xa9
>>q = 'Libert\xc3\xa9'
q.decode('utf-8')
u'Libert\xe9'
>>print q
LibertÚ
>>>
[ screen output is next line ]

LbertÚ


# history 3

# this code is run from within idle on win98 and inside a python file.
# The code DOES NOT produce the proper outout.

#_*_ coding: utf-8 _*_

# print "u'Libert\u00e9'" printed to screen

y = raw_input('Enter >')

# y = u'Lbert\u00e9' commented out

y.encode('utf-8')
q = y.encode('utf-8')
q.decode('utf-8')
print q.decode('utf-8')

# output is on the lines below was produced on the screen after run

enter u'Libert\u00e9' on screen to copy into into y string
Enter >u'Libert\u00e9'

u'Libert\u00e9'

The code DOES NOT produce LibertÚ but instead produce u'Libert\u00e9'

# history 4

# this code is run from within terminal on Debian linux inside a
python file.
# The code does not produce proper outout but produces the same output
as run on
# windows.

#_*_ coding: utf-8 _*_

print "u'Libert\u00e9'" printed to screen

y = raw_input('Enter >')

# y = u'Lbert\u00e9' commented out

y.encode('utf-8')
q = y.encode('utf-8')
q.decode('utf-8')
print q.decode('utf-8')

# output is on the lines below was produced on the screen after run

enter u'Libert\u00e9' on screen to copy into into y string
Enter >u'Libert\u00e9'
u'Libert\u00e9'

The code DID NOT produce LibertÚ but instead produce u'Libert\u00e9'
Jul 14 '08 #1
1 5607
On Jul 14, 1:51*pm, anonymous <anonym...@anonymous.comwrote:
1 Objective to write little programs to help me learn German. *See code
after numbered comments. //Thanks in advance for any direction or
suggestions.

tk

2 *Want keyboard answer input, for example: *

answer_str *= raw_input(' Enter answer ') Herr *▄Ř

[ I keyboard in the following characters Herr ▄Ř ]
print answer_str
Output on screen is Herr ▄Ř

3 * history 1 and 2 *code run interactively under Debian Linux Python
2.4 and interactively under windows98, first edition IDLE, Python 2.3.5
and it works.

4 *history 3 and 4 code run from within a .py file produce different
output from example in book.

5 want to operate under Debian Linux but because the program failed
under Linux when I tried to run the code from a file in Linux Python, I
thougt I should fire up the win98 Idle/python program and try it to see
if ran there but it failed, too from within a file.

6 The sample code is from page 108-109 of: * "Python for Dummies"
* * * It says in the book: *"Python's file objects and StringIO objects
don't support raw Unicode; the usual workaround is to encode Unicode as
UTF-8 before saving it to a file or stringIO object. *
The sample code from the book is French as indicate here but trying
German produces the same result.

7 I have searched the net under all the keywords but this is as close as
I get to accomplishing my task. *I suspect I may not be understanding:
StringIO objects don't support raw Unicode, but I don't know.

#_*_ coding: utf-8 _*_

# code run under linux debian *interactively from a terminal and works

print " u'Libert\u00e9' "

# y = raw_input('Enter >') *commented out

y = u'Lbert\u00e9'
y.encode('utf-8')
q = y.encode('utf-8')
q.decode('utf-8')
print q.decode('utf-8')

history 1 works and here is the screen copy of interactive

*>>y = raw_input ('>')
*>Libert\xc3\xa9
*>>q = 'Libert\xc3\xa9'
*>>q.decode('utf-8')
u'Libert\xe9'
*>>print q
LibertÚ
*>>>

[ *screen output is next line ]

LbertÚ

history 2
# code run under win98, first edition, within IDLE interactively and
succeeded in produce correct results.

# y = raw_input('Enter >') *commented out

y = u'Lbert\u00e9'
y.encode('utf-8')
q = y.encode('utf-8')
q.decode('utf-8')
print q.decode('utf-8')

history 1 works and here is the screen copy of interactive

*>>y = raw_input ('>')
*>Libert\xc3\xa9
*>>q = 'Libert\xc3\xa9'
*>>q.decode('utf-8')
u'Libert\xe9'
*>>print q
LibertÚ
*>>>

[ *screen output is next line ]

LbertÚ

# history 3

# this code is run from within idle on win98 and inside a python file. *
# *The code DOES NOT produce the proper outout.

#_*_ coding: utf-8 _*_

# print "u'Libert\u00e9'" *printed to screen

y = raw_input('Enter >')

# y = u'Lbert\u00e9' commented out

y.encode('utf-8')
q = y.encode('utf-8')
q.decode('utf-8')
print q.decode('utf-8')

# output is *on the lines *below was produced on the screen after run

enter u'Libert\u00e9' on screen to copy into into y string
Enter >u'Libert\u00e9'

u'Libert\u00e9'

The code DOES NOT produce LibertÚ but instead produce u'Libert\u00e9'

# history 4

# this code is run from within terminal on Debian linux * inside a
python file. *
# The code does not produce proper outout but produces the same output
as run on
# windows.

#_*_ coding: utf-8 _*_

print "u'Libert\u00e9'" *printed to screen

y = raw_input('Enter >')

# y = u'Lbert\u00e9' commented out

y.encode('utf-8')
q = y.encode('utf-8')
q.decode('utf-8')
print q.decode('utf-8')

# output is *on the lines *below was produced on the screen after run

enter u'Libert\u00e9' on screen to copy into into y string
Enter >u'Libert\u00e9'
u'Libert\u00e9'

The code DID NOT produce LibertÚ but instead produce u'Libert\u00e9'
raw_input returns what you entered. You entered u'Libert\u00e9' so
that's what was printed out.

If you want to be able to enter escape sequences like \u00e9 and have
them decoded to the appropriate character then you must do something
like this:

# The code
text = raw_input('Enter >')
decoded_text = text.decode("unicode-escape")
print decoded_text
# The output
Enter >Libert\u00e9
LibertÚ

HTH
Jul 14 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Chris McKeever | last post by:
I am trying to modify the Mailman Python code to stop mapping MIME-types and use the extension of the attachment instead. I am pretty much clueless as to what I need to do here, but I think I have...
16
by: Cameron Laird | last post by:
QOTW: "I found the discussion of unicode, in any python book I have, insufficient." -- Thomas Heller "If you develop on a Mac, ... Objective-C could come in handy. . . . PyObjC makes mixing...
9
by: thijs.braem | last post by:
Hi everyone, I'm having quite some troubles trying to convert Unicode to String (for use in psycopg, which apparently doesn't know how to cope with unicode strings). The error I keep having...
6
by: | last post by:
Hi, I used extensively python and now I find this mess with strings, I can't even reproduce tutorial examples: File "<stdin>", line 0 ^ SyntaxError: 'ascii' codec can't decode byte 0xc4 in...
3
by: bsagert | last post by:
Some web feeds use decimal character entities that seem to confuse Python (or me). For example, the string "doesn't" may be coded as "doesn’t" which should produce a right leaning apostrophe....
1
by: Mudcat | last post by:
In short what I'm trying to do is read a document using an xml parser and then upload that data back into a database. I've got the code more or less completed using xml.etree.ElementTree for the...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.