Python Cookbook 1.2 字符与数字的相互转换
2009-10-22 11:08
716 查看
需求:
需要把一个字符(ASCII或Unicode)转换为数字编码
,或者反过来转换.
讨论
:
对于ASCII字符,可以使用内建的ord和chr方法实现需求:
>>> chr(97)
'a'
>>> ord('a')
97
对于Unicode字符,需要使用ord和repr
,获得unicode字符的方法,使用unichr:
>>> print ord(u'/u2020')
8224
>>> print repr(unichr(8224))
u'/u2020'
相关说明:
下面是python中对这几个方法的简单说明:
ord(...)
ord(c) -> integer
Return the integer ordinal of a one-character string.
chr(...)
chr(i) -> character
Return a string of one character with ordinal i; 0 <= i < 256.
repr(...)
repr(object) -> string
Return the canonical string representation of the object.
For most object types, eval(repr(object)) == object.
unichr(...)
unichr(i) -> Unicode character
Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.
需要把一个字符(ASCII或Unicode)转换为数字编码
,或者反过来转换.
讨论
:
对于ASCII字符,可以使用内建的ord和chr方法实现需求:
>>> chr(97)
'a'
>>> ord('a')
97
对于Unicode字符,需要使用ord和repr
,获得unicode字符的方法,使用unichr:
>>> print ord(u'/u2020')
8224
>>> print repr(unichr(8224))
u'/u2020'
相关说明:
下面是python中对这几个方法的简单说明:
ord(...)
ord(c) -> integer
Return the integer ordinal of a one-character string.
chr(...)
chr(i) -> character
Return a string of one character with ordinal i; 0 <= i < 256.
repr(...)
repr(object) -> string
Return the canonical string representation of the object.
For most object types, eval(repr(object)) == object.
unichr(...)
unichr(i) -> Unicode character
Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.
相关文章推荐
- Python Cookbook 第二版 汉化版 [Recipe 1.2] 字符与其对应的数字编码之间的转换
- Python Cookbook 第二版 汉化版 [Recipe 1.2] 字符与其对应的数字编码之间的转换
- python cookbook 学习笔记 -- 1.2 字符与字符值间转换
- Python字符与数字的相互转换
- Python字符与数字的相互转换
- Python字符与数字的相互转换
- Python数字与字符之间的相互转换
- Python数字与字符转换
- python cookbook 学习笔记 -- 1.8 检查字符串中是否包含某字符集合中的字符
- Python 实int型和list相互转换 现把float型列表转换为int型列表 把列表中的数字由float转换为int型
- Recipe 1.2. Converting Between Characters and Numeric Codes(Python Cookbook)
- python CookBook 3 1.2 拆解任意长度的可迭代对象
- 【python cookbook】 将字符串逐字符或逐词反转
- Python Cookbook 第二版 汉化版 [Recipe 1.8] 检测字符串是否包含特定的字符集合
- python 数字类型和字符串类型的相互转换实例
- python cookbook:第三章 数字、日期和时间
- python中字符与数字的转换
- Python Cookbook 第二版 汉化版 [Recipe 1.1] 逐个处理字符串中的各个字符
- [置顶] 【C语言】接收字符,大小写字符相互转换后输出,数字不输出
- 【python cookbook】 检查字符串中是否包含某自符集合中的字符