《Modern Python Cookbook》(Python经典实例)笔记 1.10 使用键盘上没有的Unicode字符

案例背景:
Python默认支持Unicode。可用的独立Unicode字符有几百万个,这些字符中绝大多数我们使用键盘无法直接输入。

很多计算机中的字体可能在设计时就没有考虑提供这些字符,特别是Windows计算机中的字体可能无法显示这些Unicode字符。Linux和Mac OS X几乎没有关于Unicode字符的问题。

在本案例中,我们将讨论如何表示这些特别的Unicode字符。

原理:
Python使用转义序列(escape sequence)扩展普通字符。通过转义序列我们可以输入海量的 Unicode字符。转义序列以\开始,下一个字符说明Unicode字符如何表示。
常见的表示方法有两大类:

  • 根据编码进行转义:编码总是以十六进制形式出现,例如U+2680。可以使用\unnnn格式将编码填充为4位数如果编码超过4位数,可以使用\Unnnnnnnn格式将编码扩展为8位数。
  • 根据名称进行转义:\N{name},name为编码的名称。

案例:

In [1]: '\u0048\u0045\u004c\u004c\u004f'
Out[1]: 'HELLO'
In [2]: '\N{DIE FACE-1}'
Out[2]: '⚀'
In [3]: '\u2680'
Out[3]: '⚀'
In [4]: '\U00002680'
Out[4]: '⚀'
In [5]: '\U0000FFE6'
Out[5]: '₩'
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Key Features Develop succinct, expressive programs in Python Learn the best practices and common idioms through carefully explained and structured recipes Discover new ways to apply Python for the new age of development Book Description Python is the preferred choice of developers, engineers, data scientists, and hobbyists everywhere. It is a great scripting language that can power your applications and provide great speed, safety, and scalability. By exposing Python as a series of simple recipes, you can gain insight into specific language features in a particular context. Having a tangible context helps make the language or standard library feature easier to understand. This book comes with over 100 recipes on the latest version of Python. The recipes will benefit everyone ranging from beginner to an expert. The book is broken down into 13 chapters that build from simple language concepts to more complex applications of the language. The recipes will touch upon all the necessary Python concepts related to data structures, OOP, functional programming, as well as statistical programming. You will get acquainted with the nuances of Python syntax and how to effectively use the advantages that it offers. You will end the book equipped with the knowledge of testing, web services, and configuration and application integration tips and tricks. The recipes take a problem-solution approach to resolve issues commonly faced by Python programmers across the globe. You will be armed with the knowledge of creating applications with flexible logging, powerful configuration, and command-line options, automated unit tests, and good documentation. What you will learn See the intricate details of the Python syntax and how to use it to your advantage Improve your code readability through functions in Python Manipulate data effectively using built-in data structures Get acquainted with advanced programming techniques in Python Equip yourself with functional and statistical programming features Write proper tests to be sure a program works as advertised Integrate application software using Python Table of Contents Chapter 1. Numbers, Strings, and Tuples Chapter 2. Statements and Syntax Chapter 3. Function Definitions Chapter 4. Built-in Data Structures – list, set, dict Chapter 5. User Inputs and Outputs Chapter 6. Basics of Classes and Objects Chapter 7. More Advanced Class Design Chapter 8. Functional and Reactive Programming Features Chapter 9. Input/Output, Physical Format, and Logical Layout Chapter 10. Statistical Programming and Linear Regression Chapter 11. Testing Chapter 12. Web Services Chapter 13. Application Integration

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值