Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Encode differences between idle python and python

Reply
Thread Tools

Encode differences between idle python and python

 
 
pretoriano_2001@hotmail.com
Guest
Posts: n/a
 
      10-10-2006
Hello:
Under win32 XP y select python command line and execute next code with
results indicated:

Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
(Intel)] on
Type "help", "copyright", "credits" or "license" for more information.
>>> u=u''
>>> u

u'\xe1\xe9\xed\xf3\xfa'
>>> print u


>>> a=u.encode('latin-1')
>>> a

'\xe1\xe9\xed\xf3\xfa'
>>> print a

ݾ
>>> type(a)

<type 'str'>
>>> type(u)

<type 'unicode'>
>>>


using python IDLE I repeat the code, but get next differen result:
IDLE 1.2
>>> u=u''
>>> u

u'\xe1\xe9\xed\xf3\xfa'
>>> print u


>>> a=u.encode('latin-1')
>>> a

'\xe1\xe9\xed\xf3\xfa'
>>> print a


>>> type(a)

<type 'str'>
>>> type(u)

<type 'unicode'>
>>>


What do you think is happending and how can I solve this ? The IDLE
looks fine but command line has problems.

 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      10-10-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> >>> u=u''
> >>> u

> u'\xe1\xe9\xed\xf3\xfa'
> >>> print u

>
> >>> a=u.encode('latin-1')
> >>> a

> '\xe1\xe9\xed\xf3\xfa'
> >>> print a

> ݾ


That means that Python is better at guessing the correct encoding than you
are. Here's how you can make it share its secrets:

>>> import sys
>>> sys.stdout.encoding

'UTF-8' # something else on your machine (cp850, maybe)

Then you can use that encoding to print:

>>> your_encoding = sys.stdout.encoding
>>> print u"".encode(your_encoding)



On the other hand: why not always print the unicode string directly?

Peter
 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      10-10-2006
At Tuesday 10/10/2006 02:44, (E-Mail Removed) wrote:

>Hello:
>Under win32 XP y select python command line and execute next code with
>results indicated:
>
>Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
>(Intel)] on
>Type "help", "copyright", "credits" or "license" for more information.
> >>> u=u''
> >>> u

>u'\xe1\xe9\xed\xf3\xfa'
> >>> print u

>
> >>> a=u.encode('latin-1')
> >>> a

>'\xe1\xe9\xed\xf3\xfa'
> >>> print a


> >>> type(a)

><type 'str'>
> >>> type(u)

><type 'unicode'>
> >>>


Because the console code page != windows code page.
Exit Python. At the console prompt, type:
>chcp

If it says 850 - your console is using codepage 850.
Enter Python again, and replace 'latin-1' with
'cp850'. You should get the right representation.


--
Gabriel Genellina
Softlab SRL






__________________________________________________
Pregunt. Respond. Descubr.
Todo lo que queras saber, y lo que ni imaginabas,
est en Yahoo! Respuestas (Beta).
Probalo ya!
http://www.yahoo.com.ar/respuestas

 
Reply With Quote
 
pretoriano_2001@hotmail.com
Guest
Posts: n/a
 
      10-10-2006

Gabriel, Peter:
Many thanks for your clear answers!!
Best regards.

Vizcayno

 
Reply With Quote
 
Neil Cerutti
Guest
Posts: n/a
 
      10-11-2006
On 2006-10-10, (E-Mail Removed) <(E-Mail Removed)> wrote:
>
> Gabriel, Peter:
> Many thanks for your clear answers!! Best regards.


Something I've been working on is currently using the following
trick:

# Create some string of non-ASCII text in ISO 8859-1.
some_string = ''.join(chr(a) for a in range(0xc0, 0xdf)).decode('ISO 8859-1')
# Print it to stdout, converting to the terminal's encoding, replacing
# unprintable characters with '?'.
print some_string.encode(sys.stdout.encoding, 'replace')

--
Neil Cerutti
That's the biggest laughingstock I've ever heard of in my life.
--Trot Nixon
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the difference between PyPy and Python? are there lot of differences? ArrC Python 4 07-13-2011 04:31 PM
DEVELOP THE WINNING EDGE, SMALL DIFFERENCES IN YOUR PERFORMANCE CANLEAD TO LARGE DIFFERENCES IN YOUR RESULTS Home_Job_opportunity C Programming 0 01-14-2009 03:51 PM
DEVELOP THE WINNING EDGE, SMALL DIFFERENCES IN YOUR PERFORMANCE CANLEAD TO LARGE DIFFERENCES IN YOUR RESULTS Home_Job_opportunity C Programming 0 01-08-2009 04:31 PM
differences between ubuntu and fedora python Eric S. Johansson Python 0 12-07-2006 05:17 AM
urlopen differences between python 2.3.3 and python 2.2??? Chris Python 0 04-14-2004 06:46 PM



Advertisments