列表在python里是有序集合對(duì)象類(lèi)型。
列表里的對(duì)象可以是任何對(duì)象:數(shù)字,字符串,列表或者字典,元組。與字符串不同,列表是可變對(duì)象,支持原處修改的操作
python的列表是:
列表的操作和字符串大部分都相同:
合并/重復(fù):
創(chuàng)建一個(gè)列表:
>>> list=[]
>>> list=[1,2,'3',[]]
>>> list
[1, 2, '3', []]
列表取值:
>>> list[1]
2
>>> list[0:3]
[1, 2, '3']
重復(fù)列表內(nèi)容:
>>> list*3
[1, 2, '3', [], 1, 2, '3', [], 1, 2, '3', []]
使用in方法來(lái)判斷對(duì)象是否在列表中:
>>> 3 in list
False
>>> [] in list
True
循環(huán)打?。?/p>
>>> for i in list:
... print (i,end=' ')
...
1 2 3 []
迭代方式創(chuàng)建列表:
>>> list=[i*4 for i in 'ASDF' ]
>>> list
['AAAA', 'SSSS', 'DDDD', 'FFFF']
矩陣:
list=[ [1,2,3,],[4,5,6],[7,8,9] ]
>>> list
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> list[0][1]
2
>>> list[1][2]
6
列表原處修改:
>>> food=['spam','eggs','milk']
>>> food[1]
'eggs'
>>> food[1]='Eggs'
>>> food[:]
['spam', 'Eggs', 'milk']
>>>food.append('cake')
>>> food
['spam', 'Eggs', 'milk', 'cake']
>>> food.sort()
>>> food
['Eggs', 'cake', 'milk', 'spam']
>>> list1=[1,2,3]
>>> list2=[4,5,6]
>>> list1.extend(list2)
>>> list1
[1, 2, 3, 4, 5, 6]
>>> list1.pop()
6
>>> list1
[1, 2, 3, 4, 5]
>>> list1
[1, 2, 3, 4, 5]
>>> list1.reverse()
>>> list1
[5, 4, 3, 2, 1]
>>> list=[1,2,3,4,5]
>>> list.index(3)
2
>>> list.insert(2,10)
>>> list
[1, 2, 10, 3, 4, 5]
>>> list
[1, 2, 10, 3, 4, 5]
>>> del list[2]
>>> list
[1, 2, 3, 4, 5]
>>> list=['abc','aDd','ace']
>>> sorted(list)
['aDd', 'abc', 'ace']
>>> list
['abc', 'aDd', 'ace']
>>> sorted(list,key=str.lower,reverse=True)
['aDd', 'ace', 'abc']
>>> sorted(list,key=str.lower)
['abc', 'ace', 'aDd']
>>>sorted([x.lower() for x in list])
['abc', 'ace', 'add']
>>> sorted([x.lower() for x in list],reverse=True)
['add', 'ace', 'abc']
>>> info=['myname',18,[1997,9,28]]
>>> _name,_age,_birth=info
>>> _name
'myname'
>>> _age
18
>>> _birth
[1997, 9, 28]
>>> _name,_age,(_birth_y,_birth_m,_birth_d)=info
>>> _birth_y
1997
>>> _birth_m,_birth_d
(9, 28)
當(dāng)取的值不固定的時(shí)候,可以用*代替:
>>> a=['adc',122,2215,'asd@asd']
>>> a_name,*a_phone,a_mail=a
>>> a_name
'adc'
>>> a_phone
[122, 2215]
>>> from collections import deque
>>> q=deque(maxlen=3)
>>> q.append(1)
>>> q.append(2)
>>> q.append(3)
>>> q
deque([1, 2, 3], maxlen=3)
>>> q.append(4)
>>> q
deque([2, 3, 4], maxlen=3)
>>> q.appendleft('5')
>>> q
deque(['5', 2, 3], maxlen=3)
>>> from heapq import nlargest,nsmallest
>>> num=[1,4,6,7,8,8,34,64,23,7,45,34]
>>> nlargest(3,num)
[64, 45, 34]
>>> nlargest(2,num)
[64, 45]
>>> nsmallest(2,num)
[1, 4]
>>> nsmallest(4,num)
[1, 4, 6, 7]
>>> num
[1, 4, 6, 7, 8, 8, 34, 64, 23, 7, 45, 34]
>>> max(num)
64
>>> min(num)
1
>>> sum(num)
241
>>> a_info=['wanger','wangerxiao',25,'computer']
>>> _name=slice(0,2)
>>> _age=slice(2,3)
>>> _job=slice(3,4)
>>> a_info[_name]
['wanger', 'wangerxiao']
>>> a_info[_age]
[25]
>>> a_info[_job]
['computer']
>> a=[1,2,3,4,5,6,2,4,2,5,6]
>>> from collections import Counter
>>> count_word=Counter(a)
>>> count_word
Counter({2: 3, 4: 2, 5: 2, 6: 2, 1: 1, 3: 1})
>>> count_word.most_common(3)
[(2, 3), (4, 2), (5, 2)]
>>> count_word.most_common(2)
[(2, 3), (4, 2)]
字典在python里是無(wú)序集合對(duì)象類(lèi)型。
字典的值都有獨(dú)立的唯一的鍵,用相應(yīng)的鍵來(lái)取值。
python字典主要特性如下:
字典用法注意事項(xiàng):
字典的賦值:
>>> dict={'a':97,'b':98}
>>> len(dict)
2
>>> print("ascii code of 'a' is {},ascii code of 'b' is {}".format(dict['a'],dict['b']))
ascii code of 'a' is 97,ascii code of 'b' is 98
判斷特定的鍵是否存在于字典里:
>>> 'a' in dict
True
>>> 'b
>>>> 'b' is in dict
True
#更改特定鍵的值
>>> food={'eggs':3,'ham':1,'spam':4}
>>> food['ham']=2
>>> food
{'eggs': 3, 'ham': 2, 'spam': 4}
#增加新的鍵和相應(yīng)的值
>>> food['branch']=['bacon','bake']
>>> food
{'eggs': 3, 'ham': 2, 'spam': 4, 'branch': ['bacon', 'bake']}
#刪除一個(gè)字典元素
>>> del food['eggs']
>>> food
{'ham': 2, 'spam': 4, 'branch': ['bacon', 'bake']}
#清空字典所有條目
>>> dict.clear()
#刪除字典
del dict
查找字典的鍵值是否存在,如果不存在可以設(shè)置返回的值
>>> food.get('ham')
2
>>> dict.get('b')
2
>>> dict.get('0')
>>> dict.get('0','none')
'none'
創(chuàng)建字典的方法:
1.最原始的方法:
dict={'name':'wanger','age':25}
2.按鍵賦值方法:
>>> dict={}
>>> dict['name']='wanger'
>>> dict['age']=25
字典的比較:
字典的比較會(huì)比較字典的鍵,而不是字典的值,可以使用zip方式將字典的值和鍵反過(guò)來(lái),這樣就會(huì)比較值了,可以使用sorted函數(shù)對(duì)字典進(jìn)行排序
>>> dict={'a':1,'b':2,'c':3,'d':4}
>>> max(dict)
'd'
>>> min(dict)
'a'
>>> max(zip(dict.values(),dict.keys()))
(4, 'd')
>>> min(zip(dict.values(),dict.keys()))
(1, 'a')
>>> sorted(zip(dict.values(),dict.keys()))
[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]
>>> sorted(zip(dict.values(),dict.keys()),reverse=True)
[(4, 'd'), (3, 'c'), (2, 'b'), (1, 'a')]
>>> rows
[{'fname': 'Brian', 'lname': 'Jones', 'uid': 1003},
{'fname': 'David', 'lname': 'Beazley', 'uid': 1002},
{'fname': 'John', 'lname': 'Clesse', 'uid': 1001},
{'fname': 'Big', 'lname': 'Jones', 'uid': 1004}]
>>> from operator import itemgetter
>>> rows_fname=sorted(rows,key=itemgetter('fname'))
>>> rows_fname
[{'fname': 'Big', 'lname': 'Jones', 'uid': 1004},
{'fname': 'Brian', 'lname':
'Jones', 'uid': 1003},
{'fname': 'David', 'lname': 'Beazley', 'uid': 1002},
{'fname': 'John', 'lname': 'Clesse', 'uid': 1001}]
>>> rows_uid=sorted(rows,key=itemgetter('uid'))
>>> rows_uid
[{'fname': 'John', 'lname': 'Clesse', 'uid': 1001},
{'fname': 'David', 'lname': 'Beazley', 'uid': 1002},
{'fname': 'Brian', 'lname': 'Jones', 'uid': 1003},
{'fname': 'Big', 'lname': 'Jones', 'uid': 1004}]
元組與列表非常類(lèi)似,只是不能在原處更改,元祖在python里的特點(diǎn):
元祖創(chuàng)建在只有單個(gè)元素的時(shí)候,必須加逗號(hào)(,),元組里可以嵌套元組
>>> tuple=()
>>> tuple=(1,)
>>> type(tuple)
<class 'tuple'>
#這里加不加括號(hào)都一樣
>>> tuple=(1,2,'3',(4,5))
>>> tuple
(1, 2, '3', (4, 5))
>>> tuple=1,2,'3',(4,5)
>>> tuple
(1, 2, '3', (4, 5))
>>> list=[1,2,3,4]
>>> sd=tuple(list)
>>> sd
(1, 2, 3, 4)
元組的排序:
元組經(jīng)過(guò)sorted排序后,會(huì)將其轉(zhuǎn)換為列表
>>> tuple=(1,5,3,6,4,2)
>>> sorted(tuple)
[1, 2, 3, 4, 5, 6]
>>> tuple
(1, 5, 3, 6, 4, 2)
>>> tuple.index(3)
2
計(jì)算元組元素?cái)?shù)目:
>>> tuple
(1, 5, 3, 6, 4, 2)
>>> tuple.count(3)
1
元組的切片:
>>> tuple[0]
1
>>> tuple[2:]
(3, 6, 4, 2)
>>> tuple[2:3]
(3,)
>>> (1,2)+(3,4)
(1, 2, 3, 4)
>>> (1,2)*4
(1, 2, 1, 2, 1, 2, 1, 2)
>>> len(tuple)
6
set是一個(gè)無(wú)序且不重復(fù)的元素集合
集合對(duì)象十一組無(wú)序排列的可哈希的值,集合成員可以做字典中的鍵。set也支持用in 和not in操作符檢查成員,由于集合本身是無(wú)序的,不可以為集合創(chuàng)建索引或執(zhí)行切片操作,也沒(méi)有鍵可用來(lái)獲取集合中元素的值。
>>> s=set('a')
>>> a=set({'k1':1,'k2':2})
>>> b=(['y','e','d','o'])
>>> c={'a','b','c'}
>>> d={('a','b','c')}
#比較a、b集合中a中存在,b中不存在的集合
>>> a={11,22,33}
>>> b={11,23,45}
>>> a.difference(b)
{33, 22}
#找到a中存在,b中不存在的集合,并把a(bǔ)、b集合中都有的值覆蓋掉
>>> a={11,22,33}
>>> print(a.difference_update(b))
None
>>> a
{33, 22}
集合的刪除:
>>> a={11,22,33}
>>> a.discard(11)
>>> a.discard(44)
>>> a
{33, 22}
#移除不存在的元素會(huì)報(bào)錯(cuò)
>>> a={11,22,33}
>>> a.remove(11)
>>> a.remove(44)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 44
>>> a
{33, 22}
#移除末尾的元素
>>> a={11,22,33}
>>> a.pop()
33
>>> a
{11, 22}
取交集:
#取交集賦給新值
>>> a={1,2,3,4}
>>> b={6,5,4,3}
>>> print (a.intersection(b))
{3, 4}
#取交集并把交集賦給a
>>> print (a.intersection_update(b))
None
>>> a
{3, 4}
>>> a={3,4}
>>> b={6,5,4,3}
#判斷a是否與b沒(méi)有交集,有交集False,無(wú)交集True
>>> a.isdisjoint(b)
False
#判斷a是否是b的子集
>>> a.issubset(b)
True
#判斷a是否是b的父集
>>> a.issuperset(b)
False
>>> a={1,2,3,4}
>>> b={3, 4, 5, 6}
#打印不同的元素
>>> print (a.symmetric_difference(b))
{1, 2, 5, 6}
#打印不同的元素,并覆蓋到集合a
>>> print (a.symmetric_difference_update(b))
None
>>> a
{1, 2, 5, 6}
>>> a={1, 2, 5, 6}
>>> b={3, 4, 5, 6}
>>> print (a.union(b))
{1, 2, 3, 4, 5, 6}
集合的更新:
>>> a={1, 2, 5, 6}
>>> b={3, 4, 5, 6}
#把a(bǔ)、b的值合并,并把值賦給集合a
>>> a.update(b)
>>> a
{1, 2, 3, 4, 5, 6}
#添加a集合的元素
>>> a.update([7,8])
>>> a
{1, 2, 3, 4, 5, 6, 7, 8}
>>> a=set(range(5))
}
>>> li=list(a)
>>> tu=tuple(a)
>>> st=str(a)
>>> print (li)
[0, 1, 2, 3, 4]
>>> print (tu)
(0, 1, 2, 3, 4)
>>> print (st)
{0, 1, 2, 3, 4}
文件對(duì)象在python里可以作為操作系統(tǒng)上的文件的鏈接
文件對(duì)象的使用方式與之前的字符串、列表等對(duì)象不同,它是對(duì)文件的輸入、輸出進(jìn)行控制
在python里會(huì)用open函數(shù)來(lái)進(jìn)行文件的控制
在python里使用open函數(shù)可以訪問(wèn)文件。
基本格式是:open(<file_address>[,access_mode])
這里的文件地址是文本形式,在Windows里由于文件地址是使用反斜杠(),所以,可以使用r來(lái)對(duì)反斜杠不進(jìn)行轉(zhuǎn)義。
例如:
open(r'C:\mydir\myfile')
訪問(wèn)模式里是參數(shù),默認(rèn)是r(讀取)
在訪問(wèn)模式,每一種方法都有一種使用到b的方式,就是二進(jìn)制模式。
文件的讀寫(xiě)參數(shù)
操作說(shuō)明符 | 解釋 |
---|---|
r | 以只讀方式打開(kāi)文件,這是默認(rèn)模式 |
rb | 以二進(jìn)制格式打開(kāi)一個(gè)文件用于只讀。這是默認(rèn)模式 |
r+ | 打開(kāi)一個(gè)文件用于讀寫(xiě) |
rb+ | 以二進(jìn)制格式打開(kāi)一個(gè)文件用于讀寫(xiě) |
w | 打開(kāi)一個(gè)文件只用于寫(xiě)入。文件存在則覆蓋,不存在,則創(chuàng)建新文件 |
wb | 以二進(jìn)制格式打開(kāi)一個(gè)文件只用于寫(xiě)入。文件存在則覆蓋,不存在則創(chuàng)建 |
w+ | 打開(kāi)一個(gè)文件用于讀寫(xiě)。如果文件已存在則將其覆蓋,不存在則創(chuàng)建新文件。 |
wb+ | 以二進(jìn)制打開(kāi)一個(gè)文件用于讀寫(xiě)。如果該文件存在則覆蓋,不存在則創(chuàng)建 |
a | 打開(kāi)一個(gè)文件用于追加,如果文件內(nèi)容存在,則將新內(nèi)容追加到文件末尾,不存在則創(chuàng)建新文件寫(xiě)入 |
ab | 以二進(jìn)制格式打開(kāi)一個(gè)文件用于寫(xiě)入 |
a+ | 打開(kāi)一個(gè)文件用于讀寫(xiě),如果該文件存在,則會(huì)將新的內(nèi)容追加到文件末尾,如果文件不存在,則創(chuàng)建新文件用于讀寫(xiě)。 |
ab+ | 以二進(jìn)制格式打開(kāi)一個(gè)文件用于追加,文件存在將追加,不存在則創(chuàng)建新文件用于讀寫(xiě) |
例子
>>> file1=open(r'D:\ruanjian\1.txt','w')
>>> file1.write('hello,world')
11
>>> file1.close()
>>> file1=open(r'D:\ruanjian\1.txt')
>>> file1.read()
'hello,world'
#tell用于獲取文件指針位置,文件讀取之后,文件指針在最后面
>>> file1.tell()
11
>>> file1.close()
>>>> file1=open(r'D:\ruanjian\1.txt')
>>> file1.seek(6)
6
>>> file1.read(5)
'world'
當(dāng)我們要讀取前五個(gè)字符的時(shí)候可以這樣:
>>> file1=open(r'D:\ruanjian\1.txt')
>>> file1.read(5)
'hello'
>>> file1.tell()
5
當(dāng)我們要按行讀取的時(shí)候,可以使用readline和readlines方法
>>> file1=open(r'D:\ruanjian\1.txt')
>>> file1.readline()
'hello,world\n'
>>> file1.readline()
'wanger\n'
>>> file1.readline()
'asdfgghh'
>>> file1.readline()
''
>>> file1=open(r'D:\ruanjian\1.txt')
>>> file1.readlines()
['hello,world\n', 'wanger\n', 'asdfgghh']
當(dāng)我們需要寫(xiě)入到一個(gè)文件的時(shí)候,會(huì)使用w模式。當(dāng)相應(yīng)的文件存在時(shí),會(huì)覆蓋原先的文件然后寫(xiě)入,當(dāng)相應(yīng)的文件不存在時(shí)會(huì)創(chuàng)建新文件。
基本寫(xiě)入
>>> file=open(r'D:\ruanjian\1.txt','w')
>>> file.write('hello,world')
11
>>> file.write('|wanger')
7
>>> file.flush()
>>> file.close()
>>> file=open(r'D:\ruanjian\1.txt')
>>> file.read()
'hello,world|wanger'
在這里flush()方法是把緩存里的內(nèi)容寫(xiě)入硬盤(pán)中。當(dāng)運(yùn)行close()方法的時(shí)候,也會(huì)進(jìn)行同樣操作。
按列表寫(xiě)入:
writelines是把列表里的元素一個(gè)一個(gè)輸入進(jìn)去。當(dāng)然,元素里的字符串最后沒(méi)有換行,最終結(jié)果也不是換行的。
>>> list=['hello,world!\n','wanger\n','asdfgh\n']
>>> file=open(r'D:\ruanjian\1.txt','w')
>>> file.writelines(list)
>>> file.close()
>>> file=open(r'D:\ruanjian\1.txt')
>>> file.read()
'hello,world!\nwanger\nasdfgh\n'
在特定位置寫(xiě)入
當(dāng)我們輸入錯(cuò)誤的時(shí)候,可以把指針挪到最前面,然后繼續(xù)輸入。seek可以有兩個(gè)傳遞變量,只有一個(gè)變量或者第一個(gè)變量為0時(shí),就是更改當(dāng)前的指針,第二個(gè)變量為1的時(shí)候,會(huì)返回當(dāng)前指針位置,這個(gè)與tell方法同樣,最后,第一個(gè)變量為0,第二個(gè)變量為2的時(shí)候會(huì)把指針?lè)诺阶詈?/p>
>>> file=open(r'D:\ruanjian\1.txt','w')
>>> file.write('heelo')
5
>>> file.seek(0)
0
>>> file.write('hello')
5
>>> file=open(r'D:\ruanjian\1.txt')
>>> file.read()
'hello'
在最后寫(xiě)入
之前看到的w模式,當(dāng)文件是已有文件,就會(huì)刪除里面的所有內(nèi)容后再寫(xiě)入的。當(dāng)我們需要在最后添加,而不是刪除原有內(nèi)容時(shí),可以使用a模式。
>>> file=open(r'D:\ruanjian\1.txt')
>>> file.read()
'hello'
>>> file.close()
>>> file=open(r'D:\ruanjian\1.txt','a')
>>> file.write('my name is wanger')
17
>>> file=open(r'D:\ruanjian\1.txt')
>>> file.read()
'hellomy name is wanger'
在模式里,我們會(huì)看到r+,w+,a+三種模式都有讀寫(xiě)的方法。
r+模式,只能打開(kāi)已有文件,打開(kāi)時(shí)保留原有文件,對(duì)文件可讀,可寫(xiě),也可更改原有內(nèi)容。打開(kāi)是指針在文件最前面。
w+模式,打開(kāi)時(shí)沒(méi)有相應(yīng)的文件,會(huì)創(chuàng)建;有相應(yīng)的文件會(huì)覆蓋原有的內(nèi)容
a+模式,可以打開(kāi)原有文件,也可創(chuàng)建新的文件,打開(kāi)時(shí)指針為文件的最后位置。指針可以放到任何位置來(lái)讀內(nèi)容,但寫(xiě)入時(shí),指針默認(rèn)會(huì)移動(dòng)到最后,然后寫(xiě)入。
模式 | 打開(kāi)已有文件 | 打開(kāi)新的文件 | 打開(kāi)時(shí)指針位置 | 寫(xiě)入時(shí)指針位置 |
---|---|---|---|---|
r+ | 保留內(nèi)容 | 發(fā)生錯(cuò)誤 | 文件開(kāi)頭 | 當(dāng)前位置 |
w+ | 刪除內(nèi)容 | 創(chuàng)建文件 | 文件開(kāi)頭 | 當(dāng)前位置 |
a+ | 保留內(nèi)容 | 創(chuàng)建文件 | 文件尾端 | 文件尾端 |
>>> cha='啊'
>>> cha_b=cha.encode()
>>> file=open(r'D:\ruanjian\1.txt','w')
>>> file.write(cha)
1
>>> file.write(cha_b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: write() argument must be str, not bytes
>>> file.close()
>>> file=open(r'D:\ruanjian\1.txt')
>>> file.read()
'啊'
>>> file=open(r'D:\ruanjian\1.txt','wb')
>>> file.write(cha)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str'
>>> file.write(cha_b)
3
>>> file.close()
>>> file=open(r'D:\ruanjian\1.txt','rb')
>>> file.read()
b'\xe5\x95\x8a'
>>> file=open(r'D:\ruanjian\1.txt','w')
>>> file.write({'a':97})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: write() argument must be str, not dict
>>> file.write(str({'a':97}))
9
>>> file.write(str([1,2]))
6
>>> file.close()
>>> file=open(r'D:\ruanjian\1.txt')
>>> file.read()
"{'a': 97}[1, 2]"
如果要將存儲(chǔ)的字符串轉(zhuǎn)換回原來(lái)的數(shù)據(jù)類(lèi)型,可以用pickle模塊:
>>> file=open(r'D:\ruanjian\1.txt','wb')
>>> a={'a':97}
>>> pickle.dump(a,file)
>>> file.close()
>>> file=open(r'D:\ruanjian\1.txt','rb')
>>> a_=pickle.load(file)
>>> a_
{'a': 97}
需要把打印的內(nèi)容直接輸出到文件里的時(shí)候:
>>> with open (r'D:\ruanjian\1.txt','w') as f:
... print ('hello,world!',file=f)
...
>>> with open (r'D:\ruanjian\1.txt') as f:
... f.read()
...
'hello,world!\n'
因?yàn)閣方式對(duì)已存在的文件會(huì)清楚后寫(xiě)入,但有的時(shí)候我們不想覆蓋原有的文件,我們可以使用如下方式:
>>> if not os.path.exists(r'D:\ruanjian\1.txt'):
... with open(r'D:\ruanjian\1.txt','wt') as f:
... f.write('hello,world')
... else:
... print ('file already exists')
...
file already exists
在python3.x中我們也可以使用這種方式來(lái)判斷文件是否存在,存在的話(huà)會(huì)報(bào)錯(cuò),不存在的話(huà)文件可以創(chuàng)建
>>> with open(r'D:ruanjian\2.txt','xt') as f:
... f.write('hello\n')
...
6
>>> with open(r'D:ruanjian\2.txt','xt') as f:
... f.write('hello\n')
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileExistsError: [Errno 17] File exists: 'D:ruanjian\\2.txt'
文件在存儲(chǔ)時(shí)也可以壓縮存儲(chǔ),需要用到gzip或者bz2模塊,在這兩個(gè)模塊中,默認(rèn)是二進(jìn)制模式,因此需要使用wt,rt等,指定text模式。讀的時(shí)候使用rt,和read()。
壓縮級(jí)別可以用compresslevel來(lái)設(shè)置,也可以使用open里的encoding,errors,newline等。
>>> with gzip.open(r'D:\ruanjian\1.gz','wt') as f:
... f.write('text')
...
4
>>> with gzip.open(r'D:\ruanjian\1.gz','rt') as f:
... f.read()
...
'text'
>>> with bz2.open(r'D:\ruanjian\1.bz2','wt') as f:
... f.write('hello,world')
...
11
>>> with bz2.open(r'D:\ruanjian\1.bz2','rt') as f:
... f.read()
...
'hello,world'
這要用到os模塊里的方法,關(guān)于os模塊可以查看公眾號(hào)的歷史消息,對(duì)os模塊有詳細(xì)的解釋?zhuān)@里只列出一些簡(jiǎn)單的方法:
>>> import os
>>> os.getcwd()
'/root/blog'
>>> os.listdir('.')
['_config.yml', 'node_modules', '.gitignore', 'source', 'db.json', 'themes', 'package.json', 'public', 'scaffolds', '.deploy_git']
#當(dāng)需要判斷是文件時(shí)
>>> files=[file for file in os.listdir('.') if os.path.isfile(os.path.join('.',file))]
>>> files
['_config.yml', '.gitignore', 'db.json', 'package.json']
歡迎各位關(guān)注我的微信公眾號(hào)
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線(xiàn),公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。
名稱(chēng)欄目:python數(shù)據(jù)結(jié)構(gòu)之列表、字典、元組、集合-創(chuàng)新互聯(lián)
文章起源:http://www.sd-ha.com/article40/dojjho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、網(wǎng)站內(nèi)鏈、用戶(hù)體驗(yàn)、外貿(mào)網(wǎng)站建設(shè)、云服務(wù)器、網(wǎng)站維護(hù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容