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
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.
Coding Theory | Localization |
Logic | Object-Oriented Design |
Performance Optimization | Quality Control |
Reengineering | Robohelp |
Software Development | Software Reuse |
Structured Design | Testing |
Tools | UML |
Deep Learning with Python by François Chollet(12569)
Hello! Python by Anthony Briggs(9914)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9795)
The Mikado Method by Ola Ellnestam Daniel Brolund(9777)
Dependency Injection in .NET by Mark Seemann(9337)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8295)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7763)
Grails in Action by Glen Smith Peter Ledbrook(7696)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(7056)
Microservices with Go by Alexander Shuiskov(6819)
Practical Design Patterns for Java Developers by Miroslav Wengner(6736)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6677)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6413)
Angular Projects - Third Edition by Aristeidis Bampakos(6083)
The Art of Crafting User Stories by The Art of Crafting User Stories(5609)
NetSuite for Consultants - Second Edition by Peter Ries(5549)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5350)
Kotlin in Action by Dmitry Jemerov(5062)
