1、要點
(1) 在C語言中沒有字符串,只有字符,
在python中的字符串hello,在C語言中是以字符數組在內存存放['h','e','l','l','o'],如果對字符串修改,則是在內存中新開辟了一段空間進行存放。
字符串特性:一旦修改,需要重新創建。
例: "hello" + "ni" + "hao" 內存中:['h','e','l','l','o'] + ['n','i'] + ['h','a','o']
萬惡的“+”,“+”號越多,在內存中多次重復創建,浪費空間。
C語言需要手動回收,而python,C#等高級語言自帶虛擬機會進行GC垃圾回收沒有被調用的內存訪問空間。
(2) python字符串的格式化(占位符)可以節省內存空間,有如下二種方式,例:
說明:第二種format方式的效果會更好,性能更好,其實變量a的值沒有變,在格式化賦值時會在內存中新開辟空間存放。在python2.7和python3.4中測試結果相同
1
2
3
4
5
6
7
8
9
10
11
|
>>> a = 'i am %s,age is %d' >>> a % ( 'wangkai' , 33 ) 'i am wangkai,age is 33' >>> print (a) i am % s,age is % d >>> a = 'i am {0},age is {1}' >>> a. format ( 'wangkai' , 33 ) 'i am wangkai,age is 33' >>> print (a) i am { 0 },age is { 1 } |
(3) 在python中會生成一個緩存池來節省內存空間,主要存放經常用到的字符串及數字,所以在一定范圍內對變量賦同樣的值,他們的id值是一樣的,當超出這個池的時候,id值則會不同
分別在python2.7和python3.4版本中進行測試,測試結果如下:(經測試在python2.7和python3.4中效果一樣)
針對字符串,無限制
1
2
3
4
5
6
7
8
|
>>> a = 'asdfsafsafasfsafasdfasfasfasf' >>> b = 'asdfsafsafasfsafasdfasfasfasf' >>> id (a), id (b) ( 140704388394128 , 140704388394128 ) >>> a = 'ni' >>> b = 'ni' >>> id (a), id (b) ( 140704388417416 , 140704388417416 ) |
針對數字,范圍:小于-5,大于256
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
>>> a = - 5 >>> b = - 5 >>> id (a), id (b) ( 8745920 , 8745920 ) >>> a = - 6 >>> b = - 6 >>> id (a), id (b) ( 140718131946128 , 140718131946320 ) >>> aa = 256 >>> bb = 256 >>> id (aa), id (bb) ( 8754272 , 8754272 ) >>> aa = 257 >>> bb = 257 >>> id (aa), id (bb) ( 19083048 , 18637656 ) |
具體python源碼對數字的定義如下:
(4) python內部執行過程:
(5) print說明:
Python 2中的print語句被Python 3中的print()函數取代,這意味著在Python 3中必須用括號將需要輸出的對象括起來。
特別說明:經測試在python2.6、python2.7,print作為語句,但已支持括號方式,例:a = 1 print a print(a)均可;
在python3.4版本中,print作為函數,只支持括號方式。
建議:為了代碼在python2和3上的兼容性,請直接使用print函數括號方式。
2、編碼轉換
一般硬盤存儲為utf-8,讀入內存中為unicode,二者如何轉換
a = '你好' '\xe4\xbd\xa0\xe5\xa5\xbd' <type 'str'>
b = u'你好' u'\u4f60\u597d' <type 'unicode'>
a.decode('utf-8') u'\u4f60\u597d' (utf-8格式解碼為unicode)
b.encode('utf-8') '\xe4\xbd\xa0\xe5\xa5\xbd' (unicode格式加密為utf-8)
注:在python2.7版本中需要如上轉換,在腳本中如要顯示中文,
只要在文件開頭加入 # _*_ coding: UTF-8 _*_ 或者 #coding=utf-8 就行了
在python3.4以后版本,無需轉換
3、調用系統命令,并存入變量:
1.import os
a = os.system('df -Th')
b = os.popen('df -Th','r') 返回一個文件對象
2.import commands
c = commands.getoutput('df -Th') 返回一個字符串
4、sys調用
import sys
sys.exit
print sys.argv
sys.path
5、導入模板方法:
1.import sys [as newname]
多次重復使用import語句時,不會重新加載被指定的模塊,只是把對該模塊的內存地址給引用到本地變量環境。
2.from sys import argv或(*)
3.reload()
reload會重新加載已加載的模塊,但原來已經使用的實例還是會使用舊的模塊,而新生產的實例會使用新的模塊;reload后還是用原來的內存地址;不能支持from。。import。。格式的模塊進行重新加載。
建議使用第一種,第二種導入的對象或變量會與當前的變量會沖突。
6、用戶交互:
在python2.7版本中
raw_input:交互輸入內容轉化為字符串;
input:交互輸入內容不進行轉化;
在python3.4版本中只有input,想要獲取數字,需要進行int轉變。
舉例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#_*_ coding:utf-8 _*_ info = 'This var will be printed out ...' name = raw_input ( 'Please input your name:' ) age = int ( raw_input ( 'age:' )) #age = input('age:') job = raw_input ( 'Job:' ) salary = input ( 'Salary:' ) print type (age) print ''' Personal information of %s: Name: %s Age : %d Job : %s Salary: %d -------------------------- ''' % (name,name, age,job,salary) |
7、用戶輸入內容隱藏:
輸入密碼時,如果想要不可見,需要利用getpass 模塊中的 getpass方法,即:
1
2
3
4
5
|
>>> import getpass >>> pwd = getpass.getpass( "please input the passwd:" ) please input the passwd: >>> print (pwd) asdfasdfa |
8、文件操作:
python2.7版本中可以用file和open打開文件, python3.4版本中只有open
f = open('file_name','r')
g = file('file_name','r')
其中打開模式有'r','w,','a','b','+'
w:替換重寫 a:追加
b:二進制文件,主要用于跨平臺,來解決window和linux的回車換行區別
+:用于同時讀寫
* 一般會對文件讀到的第一行去掉末尾的換行符 f.readline().strip('\n')
* xreadlines:針對大文件,一行一行讀,默認是把全文件讀入內存。
* r+ :讀寫,默認從文件尾寫入,可以由seek跳到指定位置,然后替換文件內容。
初始文件aa.txt
kevin:123:1
wang:22:2
kai:311:3
對python2.7和python3.4測試結果一樣。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# _*_ coding: UTF-8 _*_ import sys,os file = sys.argv[ 1 ] f = open ( file , 'r+' ) line_list = f.readlines() new_list = [] for line in line_list: #去掉行尾的換行符 line = line.strip( '\n' ) #對行信息按分隔符進行分列 value_list = line.split( ':' ) #獲取最后一字段并數字化 last_value = int (value_list[ - 1 ]) #對最后一字段進行數字計算 last_value * = 13 value_list[ - 1 ] = str (last_value) #將列表轉變為字符串 new_str = ':' .join(value_list) #將循環的改變后的行追加到新的列表 new_list.append(new_str) ''' ######第一種方法按行追加到文件##### #按修改后的行追加到文件中 #f.writelines(new_str + '\n') ''' ''' #####第二種方法將所有行統一追加到文件##### #將所有修改后的新列表轉化為字符串 my_str = '\n'.join(new_list) #將指標移到行首 f.seek(0) #將寫回到文件 f.write(my_str + '\n') ''' f.close() |
9、類型轉變:
Python 有辦法將任意值轉為字符串:將它傳入repr() 或str() 函數。
函數str() 用于將值轉化為適于人閱讀的形式,而repr() 轉化為供解釋器讀取的形式(如果沒有等價的
語法,則會發生SyntaxError 異常) 某對象沒有適于人閱讀的解釋形式的話, str() 會返回與repr()等同的值。很多類型,諸如數值或鏈表、字典這樣的結構,針對各函數都有著統一的解讀方式。字符串和浮點數,有著獨特的解讀方式。
Some examples:
下面有些例子
1
2
3
4
5
6
7
8
9
|
>>> s = 'Hello, world.' >>> str (s) 'Hello, world.' >>> repr (s) "'Hello, world.'" >>> str ( 1.0 / 7.0 ) '0.142857142857' >>> repr ( 1.0 / 7.0 ) '0.14285714285714285' |