Home | 简体中文 | 繁体中文 | 杂文 | 知乎专栏 | 51CTO学院 | CSDN程序员研修院 | Github | OSChina 博客 | 腾讯云社区 | 阿里云栖社区 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏多维度架构

1.5. 数据类型

1.5.1. type 数据类型检测

http://docs.python.org/library/types.html

		
>>> type( [] ) == list
True
>>> type( {} ) == dict
True
>>> type( "" ) == str
True
>>> type( 0 ) == int
True
>>> class Test1 ( object ):
    pass
>>> class Test2 ( Test1 ):
    pass
>>> a = Test1()
>>> b = Test2()
>>> type( a ) == Test1
True
>>> type( b ) == Test2
True
>>> type( b ) == Test1
False
>>> isinstance( b, Test1 )
True
>>> isinstance( b, Test2 )
True
>>> isinstance( a, Test1 )
True
>>> isinstance( a, Test2 )
False
>>> isinstance( [], list )
True
>>> isinstance( {}, dict )
True
		
		
		
>>> a = []
>>> type(a)
<type 'list'>
>>> f = ()
>>> type(f)
<type 'tuple'>
		
		

1.5.2. String

1.5.2.1. String function

1.5.2.1.1. str.find()

找到字符,返回字符串位置。没有找到返回 -1

"aaa bbb ccc".find('aaa')
				
1.5.2.1.2. str.find()

查找并替换字符串

a = 'hello word'
a.replace('word','python')
				

1.5.2.2. Convert str to bytes in python

			
>>> b = str.encode(y)
>>> type(b) >>> b b’Hello World!’
To alter from bytes to str, capitalize on bytes.decode().
>>> z = b”Hello World!”
>>> y = “Hello World!”
>>> type(z)

>>> type(y)

To alter from str to bytes, capitalize on str.encode().
>>> a = bytes.decode(z)
>>> type(a)

>>> a
‘Hello World!’


# to utf-8		
'BG7NYT'.encode('utf-8')
# to utf-16
'BG7NYT'.encode('utf-16')
			
			

1.5.2.3. String format 字符串格式化输出

			
strHello = "the length of (%s) is %d" %('Hello World',len('Hello World'))
print strHello			
			
			

前导字符串加0

			
for i in range(5):
    print("%03d" % i)	

for i in range(100):
    print('{0:0>3d}'.format(i))    		
			
			
1.5.2.3.1. 格式化字典输出
				
member = {'name':'neo','age':18}
'my name is {name},age is {age}'.format(**member)        #**dict				
				
				

输出

				
>>> member = {'name':'neo','age':18}
>>> 'my name is {name},age is {age}'.format(**member) 
'my name is neo,age is 18'				
				
				

1.5.3. Array

1.5.3.1. 遍历数字

			
colours = ["RED","GREEN","BLUE"]
for colour in colours:
    print colour	
			
			

			
colours = ["RED","GREEN","BLUE"]
for i in range(0, len(colours)):
    print i, colour[i]			
			
			

1.5.3.2. split / join

			
>>> str = 'a|b|c|d|e'
>>> str.split("|")
['a', 'b', 'c', 'd', 'e']

>>> list = ['a', 'b', 'c', 'd', 'e']
>>> "|".join(list)
'a|b|c|d|e'
			
			

1.5.4. Datetime

1.5.4.1. datetime

			
from datetime import datetime
datetime.today().strftime('%Y-%m-%d.%H:%M:%S')
			
			

1.5.4.2. 字符串转日期

			
from datetime import datetime

dt_str = '27/10/20 05:23:20'
dt_obj = datetime.strptime(dt_str, '%d/%m/%y %H:%M:%S')

print("The type of the date is now",  type(dt_obj))
print("The date is", dt_obj)			
			
			

1.5.4.3. 日期格式化

			
timepoint = time.strftime('%Y-%m-%d.%H:%M:%S',time.localtime(time.time()))			
			
			

1.5.5. bytes 类型

1.5.5.1. bytes to string

		
.decode()
		
			

1.5.5.2. BOM头

BOM : fffe
BOM_BE : feff
BOM_LE : fffe
BOM_UTF8 : efbb bf
BOM_UTF16 : fffe
BOM_UTF16_BE : feff
BOM_UTF16_LE : fffe
BOM_UTF32 : fffe 0000
BOM_UTF32_BE : 0000 feff
BOM_UTF32_LE : fffe 0000
			

1.5.5.3. replace

bytes类型的数据,替换方法,不要忘记b""

			
a = b"abc"
b = a.replace(b"a", b"f")
print(b)
			
			

1.5.5.4. pack/unpack