Python 数据类型转换

sys.maxint

>>> import sys
>>> print sys.maxint
9223372036854775807

字符串转换成整数

int(s,[base])

将字符串转换成整数,如果指定转换进制,则把字符串按指定进制转换成十进制数。如果字符串不是一个有效的数值则会触发ValueError 异常

>>> int('1001',2)
9
>>> int('FF', 16)
255
>>> int('23', 8)
19
>>> int('12', 5)
7

当字符串无法转换成数字时

>>> int('anfs', 10)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'anfs'


字符串转浮点数

float(s)

根据字符串返回一个浮点数,如果不是一个有效的字符串,则会触发ValueError异常

>>> e = float('3.14159')
>>> e
3.14159
>>> type(e)
<type 'float'>

数值类型转字符串

str(X)

返回一个基于X数值的字符串,如果数值无效,则会触发异常

>>> str(3.14149)
'3.14149'
>>> str(123456)
'123456'
>>> str(add)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'add' is not defined
>>> str(123L)
'123'
>>>

列表或字符串转元组

tuple(s)

返回基于序列s的一个元组,s可以为列表或字符串,否则触发异常

>>> tuple([1, 3, 5])
(1, 3, 5)
>>> tuple('Hello World')
('H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd')
>>> tuple(123)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>>

元组或字符串转列表

list(s)


返回基于序列s的一个列表,s可以为元组或字符串,否则触发异常

>>> list((1, 2, 3))
[1, 2, 3]
>>> list('Hello')
['H', 'e', 'l', 'l', 'o']
>>> list(abc)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'abc' is not defined
>>> list(123)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable

到集合的转换

set(l)

返回一个基于序列l的集合,l可以为列表,元组或字符串

>>> set([11, 3, 7])
set([3, 11, 7])
>>> set((11, 3, 7))
set([3, 11, 7])
>>> set('Hello')
set(['H', 'e', 'l', 'o'])
>>> set(123)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable

到字典的转换

dict(s) 

s必须是以键值对的形式组合的列表或元组

>>> dict([('name', 'xiao'), ('age', 28)])
{'age': 28, 'name': 'xiao'}
>>> dict((('name', 'xiao'), ('age', 28)))
{'age': 28, 'name': 'xiao'}
>>> dict((['name', 'xiao'], ['age', 28]))
{'age': 28, 'name': 'xiao'}
>>> dict('age', 28)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: dict expected at most 1 arguments, got 2
>>> dict(['age', 28])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: dictionary update sequence element #0 has length 3; 2 is required
>>> dict([('age', 28)])
{'age': 28}
>>> dict((('age', 28)))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: dictionary update sequence element #0 has length 3; 2 is required
>>> dict([['age', 28]])
{'age': 28}

到frozenset的转换

frozenset(s)

>>> s = set([1, 2, 3])
>>> frozenset(s)
frozenset([1, 2, 3])
>>> frozenset([1, 2, 3])
frozenset([1, 2, 3])
>>> frozenset((1, 2, 3))
frozenset([1, 2, 3])
>>> frozenset('hello')
frozenset(['h', 'e', 'l', 'o'])

数字转字符

chr(x)

从整数x返回一个字符,x的取值范围是 0 ~ 255

>>> chr(0)
'\x00'
>>> chr(65)
'A'
>>> chr(255)
'\xff'
>>> chr(-1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: chr() arg not in range(256)
>>> chr(256)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: chr() arg not in range(256)

数字转unicode字符

unichr(x)

从整数返回一个unicode字符, x的取值范围在大多数系统上为 0 到65536

>>> unichr(1000)
u'\u03e8'
>>> unichr(65)
u'A'
>>> unichr(-1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: unichr() arg not in range(0x110000) (wide Python build)
>>> unichr(65536)
u'\U00010000'
>>> unichr(65537)
u'\U00010001'

返回一个字符的ASCII码

ord(c)

>>> ord('a')
97


从整数返回一个十六进制字符串

hex(x)

>>> hex(121)
'0x79'

从整数返回一个八进制字符串

oct(x)

>>> oct(121)
'0171'

从整数返回一个二进制字符串

bin(x)

>>> bin(878)
'0b1101101110'












展开阅读全文

没有更多推荐了,返回首页