Python-在Unicode和普通字符串之间转换

Python3中实现Unicode转换为可见字符串的方法 以上两种方法都是非常简单易懂的实现方式,能够很好地满足我们的需求。但需要注意的是,以上方法只适用于Unicode编码的字符串转换,如果出现其他编码方式,可能需要使用不同的解决方案。总的来说,Python内置了很多强大的编解码器,只要找到合适的编解码器,就能实现我们需要的功能。),想把这些编码字符串转换成正常的中文字符串(比如"我爱你")。在日常的开发工作中,我们经常会遇到Unicode编码的字符串(比如。同样,这段代码的输出结果也为"我爱你"。这段代码的输出结果为"我爱你"。 阅读详情

1.1. 问题 Problem

You need to deal with data that doesn't fit in the ASCII character set.

你需要处理不适合用ASCII字符集表示的数据.

1.2. 解决 Solution

Unicode strings can be encoded in plain strings in a variety of ways, according to whichever encoding you choose:

Unicode字符串可以用多种方式编码为普通字符串, 依照你所选择的编码(encoding):

   1 #将Unicode转换成普通的Python字符串:"编码(encode)"
   2 unicodestring = u"Hello world"
   3 utf8string = unicodestring.encode("utf-8")
   4 asciistring = unicodestring.encode("ascii")
   5 isostring = unicodestring.encode("ISO-8859-1")
   6 utf16string = unicodestring.encode("utf-16")
   7 
   8 
   9 #将普通的Python字符串转换成Unicode: "解码(decode)"
  10 plainstring1 = unicode(utf8string, "utf-8")
  11 plainstring2 = unicode(asciistring, "ascii")
  12 plainstring3 = unicode(isostring, "ISO-8859-1")
  13 plainstring4 = unicode(utf16string, "utf-16")
  14 
  15 assert plainstring1==plainstring2==plainstring3==plainstring4

1.3. 讨论 Discussion

If you find yourself dealing with text that contains non-ASCII characters, you have to learn about Unicode梬hat it is, how it works, and how Python uses it.

如果你发现自己正在处理包含非ASCII码字符的文本, 你必须学习Unicode,关于它是什么,如何工作,而且Python如何使用它。

Unicode is a big topic.Luckily, you don't need to know everything about Unicode to be able to solve real-world problems with it: a few basic bits of knowledge are enough.First, you must understand the difference between bytes and characters.In older, ASCII-centric languages and environments, bytes and characters are treated as the same thing.Since a byte can hold up to 256 values, these environments are limited to 256 characters.Unicode, on the other hand, has tens of thousands of characters.That means that each Unicode character takes more than one byte, so you need to make the distinction between characters and bytes.

Unicode是一个大的主题。幸运地,你并不需要知道关于Unicode码的每件事,就能够用它解决真实世界的问题: 一些基本知识就够了。首先,你得了解在字节和字符之间的不同。原先,在以ASCII码为中心的语言和环境中,字节和字符被当做相同的事物。由于一个字节只能有256个值,这些环境就受限为只支持 256个字符。Unicode码,另一方面,有数万个字符,那意谓着每个Unicode字符占用多个字节,因此,你需要在字符和字节之间作出区别。

Standard Python strings are really byte strings, and a Python character is really a byte.Other terms for the standard Python type are "8-bit string" and "plain string.",In this recipe we will call them byte strings, to remind you of their byte-orientedness.

标准的Python字符串确实是字节字符串,而且一个Python字符真的是一个字节。换个术语,标准的 Python字符串类型的是 "8位字符串(8-bit string)"和"普通字符串(plain string)". 在这一份配方中我们把它们称作是字节串(byte strings), 并记住它们是基于字节的。

Conversely, a Python Unicode character is an abstract object big enough to hold the character, analogous to Python's long integers.You don't have to worry about the internal representation;the representation of Unicode characters becomes an issue only when you are trying to send them to some byte-oriented function, such as the write method for files or the send method for network sockets.At that point, you must choose how to represent the characters as bytes.Converting from Unicode to a byte string is called encoding the string.Similarly, when you load Unicode strings from a file, socket, or other byte-oriented object, you need to decode the strings from bytes to characters.

相反地,一个Python Unicode码字符是一个大到足够支持(Unicode)字符的一个抽象对象, 类似于Python中的长整数。 你不必要为内在的表示担忧; 只有当你正在尝试把它们传递给给一些基于字节的函数的时候,Unicode字符的表示变成一个议题, 比如文件的write方法或网络套接字的send 方法。那时,你必须要选择该如何表示这些(Unicode)字符为字节。从Unicode码到字节串的转换被叫做编码。同样地,当你从文件,套接字或其他的基于字节的对象中装入一个Unicode字符串的时候,你需要把字节串解码为(Unicode)字符。

There are many ways of converting Unicode objects to byte strings, each of which is called an encoding.For a variety of historical, political, and technical reasons, there is no one "right" encoding.Every encoding has a case-insensitive name, and that name is passed to the decode method as a parameter. Here are a few you should know about:

将Unicode码对象转换成字节串有许多方法, 每个被称为一个编码(encoding)。由于多种历史的,政治上的,和技术上的原因,没有一个 "正确的"编码。每个编码有一个大小写无关的名字,而且那一个名字被作为一个叁数传给解码方法。这里是一些你应该知道的:

  • The UTF-8 encoding can handle any Unicode character.It is also backward compatible with ASCII, so a pure ASCII file can also be considered a UTF-8 file, and a UTF-8 file that happens to use only ASCII characters is identical to an ASCII file with the same characters.This property makes UTF-8 very backward-compatible, especially with older Unix tools.UTF-8 is far and away the dominant encoding on Unix.It's primary weakness is that it is fairly inefficient for Eastern texts.
  • UTF-8 编码能处理任何的Unicode字符。它也是与ASCII码向后兼容的,因此一个纯粹的ASCII码文件也能被考虑为一个UTF-8 文件,而且一个碰巧只使用ASCII码字符的 UTF-8 文件和拥有同样字符的ASCII码文件是相同的。这个特性使得UTF-8的向后兼容性非常好,尤其使用较旧的 Unix工具时。UTF-8 无疑地是在 Unix 上的占优势的编码。它主要的弱点是对东方文字是非常低效的。
  • The UTF-16 encoding is favored by Microsoft operating systems and the Java environment.It is less efficient for Western languages but more efficient for Eastern ones.A variant of UTF-16 is sometimes known as UCS-2.
  • UTF-16 编码在微软的操作系统和Java环境下受到偏爱。它对西方语言是比较低效,但对于东方语言是更有效率的。一个 UTF-16 的变体有时叫作UCS-2 。
  • The ISO-8859 series of encodings are 256-character ASCII supersets.They cannot support all of the Unicode characters;they can support only some particular language or family of languages.ISO-8859-1, also known as Latin-1, covers most Western European and African languages, but not Arabic.ISO-8859-2, also known as Latin-2,covers many Eastern European languages such as Hungarian and Polish.
  • ISO-8859编码系列是256个字符的ASCII码的超集。他们不能够支援所有的Unicode码字符; 他们只能支援一些特别的语言或语言家族。ISO-8859-1, 也既Latin-1,包括大多数的西欧和非洲语言, 但是不含阿拉伯语。ISO-8859-2,也既Latin-2,包括许多东欧的语言,像是匈牙利语和波兰语。

If you want to be able to encode all Unicode characters, you probably want to use UTF-8.You will probably need to deal with the other encodings only when you are handed data in those encodings created by some other application.

如果你想要能够编码所有的Unicode码字符,你或许想要使用UTF-8。只有当你需要处理那些由其他应用产生的其它编码的数据时,你或许才需要处理其他编码。

Python Cookbook-1.21 在Unicode和普通字符串之间转换 需要处理一些可能不符合ASCII字符集的文本数据。 阅读详情

相关推荐

深入剖析STM32F103 I2C总线死锁问题及高效应对策略

本文深入剖析了STM32F103硬件I2C总线在轮询模式下发生死锁的根本原因,即硬件状态机的预取机制与软件响应速度不匹配。文章提供了高效的解决方案,包括在轮询模式下提前发送NACK/STOP信号、降低总线速度,以及治本的中断与DMA应用策略,帮助开发者根据项目需求选择最合适的方案,彻底解决这一经典硬件缺陷问题。

weixin_29184371的博客 212

将Unicode转换成普通的Python字符串:“编码(encode)“

1 、将Unicode转换成普通的Python字符串:“编码(encode)” unicodestring = u"Hello world" utf8string = unicodestring.encode("utf-8") asciistring = unicodestring.encode("ascii") isostring = unicodestring.encode("ISO-8859-1") utf16string = unicodestring.encode("utf-16") 2、将普通

longe20111104的博客 3166

ArcGIS+PLUS+InVEST三件套实战:从零搞定土地利用变化与碳储量预测(附完整数据包)

本文详细介绍了如何整合ArcGIS、PLUS模型和InVEST模型,构建土地利用变化与碳储量预测的完整研究流水线。通过多情景模拟和生态系统服务评估,提供从数据准备到结果验证的实战指南,助力研究者高效完成生态评估项目。

weixin_30399821的博客 549

Python: 在Unicode和普通字符串之间转换

2019独角兽企业重金招聘Python工程师标准>>> ...

weixin_34228617的博客 899

用户行为数据分析 User Behavior Data from Taobao for Recommendation

一、提出问题 二、理解数据 数据来源 阿里云天池数据集:(https://tianchi.aliyun.com/dataset/dataDetail?dataId=649) 本文选取的数据集为UserBehavior.csv,共有5242880条数据。数据的具体类别如下表所示: 用户行为种类有四种,如下表所示 对于数据维度的说明如下: 在本文选取的2017年11月25日到2017年12月3日的时间范围内,不重复的用户数量为10202,不重复的商品ID数为412369,不重复的商品类目ID数为5859,

cccxxxqq的博客 5222

英飞凌TC3xx的GTM模块到底有多强?从PWM生成到电机驱动,一篇讲透车载定时器核心

本文深入解析英飞凌TC3xx系列中的GTM模块在汽车电子领域的强大功能,从PWM生成到电机驱动的全链路设计。通过模块化架构和硬件级解决方案,GTM显著提升系统精度和响应速度,适用于电机控制、电源管理等复杂场景。文章结合实战经验,详细拆解GTM的技术实现和性能优势。

weixin_27741699的博客 220

使用python将unicode转码为字符串

在Python中,将Unicode编码转换为字符串可以使用内置函数chr()。chr()函数接受一个整数参数,该整数表示Unicode编码,返回对应的Unicode字符。 以下是将Unicode编码转换为字符串的示例代码: unicode_value = 233# 233是Unicode编码值 string_value = chr(unicode_value) # 将Unicode编码转换为字符...

weixin_35757191的博客 3753

YOLOv8剪枝实战:基于torch_pruning的模型优化与性能提升指南

本文详细介绍了如何使用torch_pruning工具对YOLOv8模型进行结构化剪枝,以实现模型轻量化与性能提升。通过实战步骤解析,包括环境搭建、依赖图分析、迭代剪枝与微调策略,帮助开发者有效减少模型参数量和计算量,提升在边缘设备上的推理速度,同时保持检测(detect)与分割(segment)任务的精度。

water的专栏 1130

python 转unicode_在python中转换unicode字符串

{u'Status': u'OK', u'City': u'Ciri\xe8', u'TimezoneName': '', u'ZipPostalCode': '', u'CountryCode': u'IT', u'Dstoffset': u'0', u'Ip': u'x.x.x.x', u'Longitude': u'7.6', u'CountryName': u'Italy', u'Regi...

weixin_39708557的博客 750

OrCAD Capture CIS 17.4 原理图绘制

OrCAD Capture CIS 17.4 原理图绘制

qq_38960013的博客 7662

Cocos Creator实战:5步搞定棋牌游戏大厅场景开发(附完整代码)

本文详细介绍了使用Cocos Creator 3.8开发棋牌游戏大厅的5个关键步骤,包括工程配置、动态UI系统实现、核心交互功能开发、性能优化及调试发布技巧。通过模块化代码和实战案例,帮助开发者快速构建高交互性的棋牌游戏大厅,提升用户留存率。

weixin_29002209的博客 368

python unicode转字符串_Python——在Unicode和普通字符串之间转换

1.1. 问题 ProblemYou need to deal with data that doesn't fit in the ASCII character set.你需要处理不适合用ASCII字符集表示的数据.1.2. 解决 SolutionUnicode strings can be encoded in plain strings in a variety of ways, accor...

weixin_39921023的博客 3039

xilinx原语及bank简介

图8是HP bank与HR bank的IO模块,其中PAD是FPGA的金属管脚,IOB是输入、输出引脚的控制模块。此时使用原语就可以只调用需要的组件,不需要去考虑其余组件参数设置,就方便很多,借助此次机会就对IDDR、ODDR、IDELAYE2、ODELAYE2、IDELAYCRTL、ISERDESE、OSERDESE等几个原语做了简要了解,并对原语的各个模式进行了仿真,本文最初是介绍IDDR的,但在介绍IDDR这些原语之前,还需要了解一下7系列FPGA bank的分类和区别,所以就有了本文。

qq_43416206的博客 427

python unicode转字符串_Python: 在Unicode和普通字符串之间转换

Unicode字符串可以用多种方式编码为普通字符串, 依照你所选择的编码(encoding):Toggle line numbers1 #将Unicode转换成普通的Python字符串:"编码(encode)"2 unicodestring = u"Hello world"3 utf8string = unicodestring.encode("utf-8")4 asciistring = uni...

weixin_39838328的博客 3200

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

InTheHand.Net.Personal+BlueTooth4,蓝牙开发

项目中有蓝牙开发的必备dll,现在网上相关的文档博客不少,有的说的多有的说少,但是却很少有这两个dll,分享一下。

python unicode函数_Python: 在Unicode和普通字符串之间转换

1.1. 问题 ProblemYou need to deal with data that doesn't fit in the ASCII character set.你需要处理不适合用ASCII字符集表示的数据.1.2. 解决 SolutionUnicode strings can be encoded in plain strings in a variety of ways, accor...

weixin_39623082的博客 560

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

Python——在Unicode和普通字符串之间转换

1.1. 问题 Problem You need to deal with data that doesn't fit in the ASCII character set. 你需要处理不适合用ASCII字符集表示的数据. 1.2. 解决 Solution Unicode strings can be encoded in plain strings in a variety ...

weixin_30786657的博客 316

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

Python 普通字符串和Unicode之间进行转换

1.1. 问题 Problem You need to deal with data that doesn't fit in the ASCII character set. 你需要处理不适合用ASCII字符集表示的数据. 1.2. 解决 Solution Unicode strings can be encoded in plain strings in a variety of way

一格子休的专栏 1111

python 在Unicode和普通字符串 str 之间转换

unicodestring = u"Hello world" # 将Unicode转化为普通Python字符串:"encode" utf8string = unicodestring.encode("utf-8") asciistring = unicodestring.encode("ascii") isostring = unicodestring.encode("ISO-88...

weixin_34414650的博客 1074

python字符编码转换_Python: 在Unicode和普通字符串之间转换

1.1. 问题ProblemYou need to deal with data that doesn't fit inthe ASCII character set.你需要处理不适合用ASCII字符集表示的数据.1.2. 解决SolutionUnicode strings can be encoded in plain stringsin a variety of ways, according...

weixin_39857480的博客 490

《Python Cookbook(第2版)中文版》——1.21 在Unicode和普通字符串之间转换

本节书摘来自异步社区《Python Cookbook(第2版)中文版》一书中的第1章,第1.21节,作者[美]Alex Martelli , Anna Martelli Ravenscrof , David Ascher ,高铁军 译,更多章节内容可以访问云栖社区“异步社区”公众号查看。 1.21 在Unicode和普通字符串之间转换 任务 需要处理一些...

weixin_33769207的博客 127

001_021 Python 在Unicode和普通如ANSI编码之间转化

代码如下: #encoding=utf-8 print '中国' #在Unicode和普通如ANSI编码之间转化 ustr=u'abc' ustr2=u'abc中国' #一 unicode 转换为普通字符串 print ustr.encode('ascii') #print ustr2.encode('ascii') # 这个失败 包含ascii表示不了的字符 print ustr2

Full in love with code 8081

python2 unicode编码转换中文_Python2.X如何将Unicode中文字符串转换成 string字符串

普通字符串可以用多种方式编码成Unicode字符串,具体要看你究竟选择了哪种编码:unicodestring = u"Hello world"# 将Unicode转化为普通Python字符串:"encode"utf8string = unicodestring.encode("utf-8")asciistring = unicodestring.encode("ascii")isostring =...

weixin_39639686的博客 138
上一篇: xlearn初探
sheldonwong
博客等级 码龄14年 5粉丝 64原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值