python中的unicode

2023-11-19

Python’s Unicode string type stores characters from the Unicode character set. In this set, each distinct character has its own number, the code point. Unicode supports more than one million code points. Unicode characters don’t have an encoding; each character is represented by its code. The Unicode string type uses some unknown mechanism to store the characters; in your Python code, Unicode strings simply appear as sequences of characters, just like 8-bit strings appear as sequences of bytes.

Observations:

  1. Text files always contain encoded text, not characters. Each character in the text is encoded as one or more bytes in the file.

  2. Most popular encodings (UTF-8, ISO-8859-X, etc) are supersets of ASCII. This means that the first 128 characters have the usual meaning, and that the usual characters are used for line endings. In other words, readline() will work just fine.

  3. You can mix Python Unicode strings with 8-bit Python strings, as long as the 8-bit string only contains ASCII characters. A Unicode-aware library may chose to use 8-bit strings for text that only contains ASCII, to save space and time.

  4. If you read a line of text from a file, you get bytes, not characters.

  5. To decode an encoded string into a string of well-defined characters, you have to know what encoding it uses.

  6. To decode a string, use the decode() method on the input string, and pass it the name of the encoding:

        fileencoding = "iso-8859-1"

    raw = file.readline()
    txt = raw.decode(fileencoding)

    (the result is a Python Unicode string).

    The decode method was added in Python 2.2. In earlier versions (or if you think it reads better), use the unicode constructor instead:

       txt = unicode(raw, fileencoding)
  7. Python’s regular expression engine supports Unicode. You can apply the same pattern to either 8-bit (encoded) or Unicode strings. To create a regular expression pattern that uses Unicode character classes for /w (and /s, and /b), use the “(?u)” flag prefix, or the re.UNICODE flag:

        pattern = re.compile("(?u)pattern")

    pattern = re.compile("pattern", re.UNICODE)
  8. To write a Unicode string to a file or other device, you have to convert it to the encoding used by the file. The encode method converts from Unicode to an encoded string.

        out = txt.encode(encoding)

    If the string contains characters that cannot be represented in the given encoding, Python raises an exception. You can change this by passing in a second argument to encode:

        # skip bad chars
    out = txt.encode(encoding, "ignore")

    # replace bad chars with "?"
    out = txt.encode(encoding, "replace")

    For more on string encoding, see Converting Unicode Strings to 8-bit Strings.

  9. To print a Unicode string to your output device, you have to convert it to the encoding used by your terminal. The encode() method converts from Unicode back to an encoded string. You can use the locale.getdefaultlocale() function to get the current output encoding.
        import locale
    language, output_encoding = locale.getdefaultlocale()

    print txt.encode(output_encoding)

There are lots of shortcuts in Python, including coded streams, using default locales for pattern matching, ISO-8859-1 as a subset of Unicode, etc, but that’s outside the scope of this note. At least for the moment.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

python中的unicode 的相关文章

随机推荐

  • 离散数学---期末复习知识点

    一 数理逻辑 复习知识点 1 命题与联结词 否定 析取 合取 蕴涵 等价 命题 非真既假的陈述句 复合命题 由简单命题通过联结词联结而成的命题 2 命题公式与赋值 成真 成假 真值表 公式类型 重言 矛盾 可满足 公式的基本等值式 3 范式
  • 机器学习编程作业-逻辑回归

    逻辑回归 作业说明 复习 任务一 使用逻辑回归辨别真假钞票 任务二 使用逻辑回归对电影评论分类 作业说明 任务一 使用逻辑回归辨别真假钞票 钞票数据集 Banknote Dataset 涉及根据给定钞票的数个度量的照片预测是真钞还是假钞 它
  • vue-element-admin项目

    vue element admin vue element admin 介绍 功能 前序准备 目录结构 项目运行机制和代码注释 main js App vue permission js Vuex结构 scss icons 安装 Contr
  • 密码学之DES算法简述(1)

    1 DES算法描述 1 输入64位明文数据 并进行初始置换IP 2 在初始置换IP后 明文数据再被分为左右两部分 每部分32位 以L0 R0表示 3 在秘钥的控制下 经过16轮运算 f 4 16轮后 左 右两部分交换 并连接再一起 再进行逆
  • MyBatis中将结果集封装到指定类型详解(反射Class.forName详解)

    文章目录 1 将结果集封装到指定类型中 1 1指定类型 1 2将信息存入到mapper中 1 3反射 1 3 1获取全限定类名 加载类并且实例化 1 3 2执行SQL语句 生成的结果集获取元信息 把每列的数据存入到指定类型中 1 4Clas
  • java正则表达式语法详解及其使用代码实例

    代码下载地址 http www zuidaima com share 1835085544524800 htm 原文 java正则表达式语法详解及其使用代码实例 Regular Expressions of Java Tutorial 译者
  • Community宣言

    Community宣言 一个幽灵 共产主义的幽灵 在欧洲游荡 为了对这个幽灵进行神圣的围剿 旧欧洲的一切势力 教皇和沙皇 梅特涅和基佐 法国的激进派和德国的警察 都联合起来了 有哪一个反对党不被它的当政的敌人骂为Community呢 又有哪
  • 【python知识点】锦集

    版权声明 未经博主同意 谢绝转载 请尊重原创 博主保留追究权 https blog csdn net m0 69908381 article details 132368704 出自 进步 于辰的博客 相关博文 python细节 经验 锦集
  • 老电脑如何用U盘重装系统?老电脑用U盘重装系统教程

    老电脑如何用U盘重装系统 用户利用U盘来给老电脑重装系统 能够帮助解决老电脑运行缓慢 系统出现故障或感染病毒等问题 通过重装系统 可以清除旧的系统文件和应用程序 重新安装一个干净且高效的操作系统 那么具体要如何操作 可以参考以下小编分享的老
  • 微信小程序开发(八)button按钮去除圆角

    背景 小程序使用button 会有一个默认圆角 如果修改数值比较容易 想要去掉就可能遇到坑 原因 小程序的设计风格是button的border和圆角等都是通过after写的 如果需要去掉圆角效果 最好是使用如下代码 button borde
  • MFC多线程编程之一——问题提出

    原文地址 http www vckbase com document viewdoc id 1704 一 问题的提出 编写一个耗时的单线程程序 新建一个基于对话框的应用程序SingleThread 在主对话框IDD SINGLETHREAD
  • 一步一步分析讲解神经网络基础-Feedforward Neural Network

    A feedforward neural network is an artificial neural network wherein connections between the units do not form a cycle A
  • linux执行makefile文件或目录,makefile.am_makefile.in文件_linux怎么用makefile

    默认的安装路径 1 标准安装路径 默认安装路径为 prefix usr local 可以通过 configure prefix 的方法来覆盖 其它的预定义目录还包括 bindir prefix bin libdir prefix lib d
  • c++中的时间处理(3)与sleep相关的时间函数

    1 Sleep 函数 头文件 Windows下为 windows h Linux下为 unistd h 注意 1 Sleep是区分大小写的 有的编译器是大写 有的是小写 2 Sleep括号里的时间 在windows下是已毫秒为单位 而Lin
  • MySQL性能分析工具的使用

    1 数据库服务器的优化步骤 当我们遇到数据库调优问题的时候 该如何思考呢 这里把思考的流程整理成下面这张图 整个流程划分成了 观察 Show status 和 行动 Action 两个部分 字母 S 的部分代表观察 会使用相应的分析工具 字
  • AngularJS API

    AngularJS提供了如下的一下常用函数 API名称 描述 anguler lowercase 转换为小写字母 anguler uppercase 转换为大写字母 angular isString 是否为字符串 isNumber 是否为数
  • 常用的转义字符 C语言

    转义字符 转义字符是一种特殊的字符常量 以反斜线 开头 后跟字符 具有特定的含义 不同于字符原有的含义 故称 转义 字符 上表 转义字符 含义 n 回车换行 光标移到下一行的行首 r 回车 光标移到当前行的行首 把当前行前面全部删掉 t 制
  • 【pyqt5学习】——菜单栏(QMenu())、工具栏QToolBar学习

    目录 1 菜单栏 QMenu 一般在窗口顶部 1 创建菜单栏步骤 2 信号与方法 3 实操 2 工具栏 一般在菜单栏下方 1 创建步骤 2 方法与信号 信号 方法 3 实操示例 3 状态栏QStateBar 用于显示状态信息 一般在窗口底部
  • 微信开放平台接入问题

    1 errcode 40163 errmsg code been used rid xxxxx 原因 获取token时 使用的code码被二次使用 2 errcode 40249 errmsg this template msg has b
  • python中的unicode

    Python s Unicode string type stores characters from the Unicode character set In this set each distinct character has it