Learning Python ( 5th Edition) by Mark Lutz

Learning Python ( 5th Edition) by Mark Lutz

Author:Mark Lutz
Language: eng
Format: mobi
Tags: code, language, python, coding, programming, computer
Published: 1999-09-10T16:00:00+00:00


754 | Chapter 25: Advanced Module Topics

www.it-ebooks.info

duced in Chapter 4, 3 .X b'...' bytes literals are taken as simple strings in 2 .X, and 2 .X u'...' Unicode literals as treated as normal strings in 3.X as of 3.3.

$54,321.99 54,321.99 £54,321.99 ¥54,321.99 £54,321.99

€54,321.99 €54,321.99 »54,321.99

If this works on your computer, you can probably skip the next few paragraphs. Depending on your interface and system settings, though, getting this to run and display properly may require additional steps. On my machine, it behaves correctly when Python and the display medium are in sync, but the euro and generic currency symbols in the last two lines fail with errors in a basic Command Prompt on Windows.

Specifically, this test script always runs and produces the output shown in the IDLE GUI in both 3.X and 2.X, because Unicode-to-glyph mappings are handled well. It also works as advertised in 3.X on Windows if you redirect the output to a file and open it with Notepad, because 3.X encodes content on this platform in a default Windows format that Notepad understands:

c:\code> formats_currency.py > temp c:\code> notepad temp

However, this doesn't work in 2.X, because Python tries to encode printed text as ASCII by default. To show all the non-ASCII characters in a Windows Command Prompt window directly, on some computers you may need to change the Windows code page (used to render characters) as well as Python's PYTHONIOENCODING environment variable (used as the encoding of text in standard streams, including the translation of characters to bytes when they are printed) to a common Unicode format such as UTF-8:

c: \code> chcp 65001 # Console matches Python

c:\code> set PYTH0NI0ENC0DINC=utf-8 # Python matches console

c:\code> formats_currency.py > temp # Both 3.X and 2.X write UTF-8 text

c: \code> type temp # Console displays it properly

c: \code> notepad temp # Notepad recognizes UTF-8 too

You may not need to take these steps on some platforms and even on some Windows distributions. I did because my laptop's code page is set to 437 (U.S. characters), but your code pages may vary.

Subtly, the only reason this test works on Python 2.X at all is because 2.X allows normal and Unicode strings to be mixed, as long as the normal string is all 7-bit ASCII characters. On 3.3, the 2.X u'...' Unicode literal is supported for compatibility, but taken the same as normal '...' strings, which are always Unicode (removing the leading u makes the test work in 3.0 through 3.2 too, but breaks 2.X compatibility): c:\code> py -2

»> print u'\xA5' + "l", '%s2' % u'\iK)0A3' # 2.X; unicode/str mix for ASCII str ¥1 £2

c:\code> py -3

Example: Dual Mode Code | 755

www.it-ebooks.info

>>> print(u'\xA5' + '1', '%s2' % u'\uOOA3') # 3.X: str is Unicode, u" optional ¥l £2

»> print('\xA5' + '1', '%s2' % '\uOOA3')

¥1 £2

Again, there's much more on Unicode in Chapter 37—a topic many see as peripheral, but which can crop up even in relatively simple contexts like this! The takeaway point here is that, operational issues aside, a carefully coded script can often manage to support Unicode in both 3.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.