zoukankan      html  css  js  c++  java
  • Python美味食谱: 1.2 字符和字符值间转换


    目的

    将一个字符转化为相应的ASCII或Unicode码,或相反的操作。

    方法

    对于ASCII码(0~255范围)

    >>> print ord('A')
    65
    >>> print chr(65)
    A


    对于Unicode字符,注意仅接收长度为1的Unicode字符

    >>> print ord(u'\u54c8')
    21704
    >>> print unichr(21704)

    >>> print repr(unichr(21704))
    u
    '\u54c8'

     

    chr()和str()区别,一个仅接收0~255的数值返回对应于ASCII值的字符,一个接受任何类型返回字符串格式

    >>> chr(97)
    'a'
    >>> str(97)
    '97'

    使用map和以上函数,来获得包含字符值或者码值的列表

    >>> print map(ord,(u'\u54c8',u'\u54c9'))
    [
    2170421705]

    >>> print map(unichr,range(21704,21707))
    [u
    '\u54c8', u'\u54c9', u'\u54ca']
  • 相关阅读:
    MongoDB 与 MySQL 性能比较
    PySpider简易教程
    使用redis有什么缺点
    禅道
    Shell02
    Shell01
    性能测试06
    性能测试05
    性能测试04
    性能测试03
  • 原文地址:https://www.cnblogs.com/yuxc/p/2131353.html
Copyright © 2011-2022 走看看