473,890 Members | 1,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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('Ente r >') 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\x a9
>>q = 'Libert\xc3\xa9 '
q.decode('u tf-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('Ente r >') 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\x a9
>>q = 'Libert\xc3\xa9 '
q.decode('u tf-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('Ente r >')

# 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('Ente r >')

# 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 5787
On Jul 14, 1:51*pm, anonymous <anonym...@anon ymous.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('Ente r >') *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\xa 9
*>>q = 'Libert\xc3\xa9 '
*>>q.decode('ut f-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('Ente r >') *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\xa 9
*>>q = 'Libert\xc3\xa9 '
*>>q.decode('ut f-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('Ente r >')

# 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('Ente r >')

# 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('Ente r >')
decoded_text = text.decode("un icode-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
2422
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 narrowed it down to the Scrubber.py file.. If this seems like a quick step me through, I would be very appreciative, could get you something on your Amazon wish-list (that is me on my knees begging).. From just my basic understanding, it...
16
2111
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 the two languages dead easy and more convenient than indoor plumbing." -- Robert Kern Among other activities, the PSF aggregates donors with dollars destined to do good Python works, and developers expert in
9
15748
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 is something like this: ERREUR: Séquence d'octets invalide pour le codage «UTF8» : 0xe02063 (sorry, locale is french, it means "byte sequence invalid for encoding
6
1889
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 position 1: ordinal not in range(128) Is there any good guide to this mess of codecs and hell ?
3
2078
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. Python hates decimal entities beyond 128 so it chokes unless you do something like string.encode('utf-8'). Even then, what should have been a right-leaning apostrophe ends up as "’". The following script does just that. Look for the string "The...
1
3867
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 parser and dbi/ odbc for my db connection. To fix problems with unicode I built a work-around by mapping unicode characters to equivalent ascii characters and then encoding everything to ascii. That allowed me to build the application and debug...
0
9978
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9820
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11228
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10814
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10922
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6041
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4270
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3276
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.