《Modern Python Cookbook》(Python经典实例)笔记 1.11 编码字符串——创建ASCII和UTF-8字节

Unicode字符通常被编码为字节序列。这些字符中既有很多标准编码,也有很多非标准编码。
Python通常使用操作系统的默认编码处理文件和互联网通信,每个操作系统的处理细节各不相同。
使用PYTHONIOENCODING环境变量进行通用设置。在Python之外设置这个变量可以保证在操作系统范围内使用特定的编码。

  • Linux操作系统环境变量的设置命令如下:export PYTHONIOENCODING=UTF-8
  • Windows操作系统环境变量的设置方法:我的电脑\计算机高级系统配置环境变量高级系统配置新建

Unicode定义了许多编码方案,其中UTF-8是最流行的,其他编码方案还有UTF-16和UTF-32。编码方案名称中的数字是该方案中每个字符的位数。一个包含1000个UTF-32编码字符的文件将有4000字节。一个包含1000个UTF-8编码字符的文件可能只有1000字节,具体的字节数取决于字符的精确组合,因为在UTF-8编码方案中,字符编码大于U+007F的字符需要使用多个字节表示。

不同的操作系统有各自的编码方案,Mac OS X文件通常使用MacRoman或latin-1编码,Windows文件可能使用CP1252编码。

这些编码方案的关键在于可以映射到Unicode字符的字节序列。另一种方法是把每个Unicode字符映射到一个或多个字节。理想情况下,所有的Unicode字符都被编码。实际上,其中一些编码方案是不完整的。编码方案最棘手的问题在于避免写入多余的字节。

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值