python代碼實(shí)現(xiàn)備忘錄案例講解
文件操作
TXT文件
讀取txt文件
讀取txt文件全部?jī)?nèi)容:
def read_all(txt):
...: with open(txt,'r') as f:
...:return f.read()
...:
read_all('test.txt')
Out[23]: 'a,b,c,d\ne,f,g,h\ni,j,k,l\n'
按行讀取txt文件內(nèi)容
def read_line(txt):
...: line_list = []
...: with open(txt,'r') as f:
...:for line in f.readlines():
...: line_list.append(line)
...: return line_list
...:
read_line('test.txt')
Out[27]: ['a,b,c,d\n', 'e,f,g,h\n', 'i,j,k,l\n']
保存文件
直接保存字符串。
str = 'aaaabbbbcc'
with open('test.txt','w') as f:
...: f.write(str)
...:
with open('test.txt','r') as f:
...: print(f.read())
...:
aaaabbbbcc
將列表中內(nèi)容寫(xiě)入txt文件。
直接寫(xiě)入
data = ['a','b','c']
...: with open("data.txt","w") as f:
...: f.writelines(data)
...:
with open('data.txt','r') as f:
...: print(f.read())
...:
abc
按行寫(xiě)入。
data = ['a','b','c']
with open('data.txt','w')as f:
...: for i in data:
...:i = str(i)+'\n'
...:f.write(i)
with open('data.txt','r') as f:
...: print(f.read())
...:
a
b
c
CSV文件
讀取csv文件
使用python內(nèi)置csv讀取.csv文件內(nèi)容。
import csv
with open('test.csv', 'r') as f:
data = csv.reader(f)
print(next(data))
['filename', 'label']
寫(xiě)入csv文件
使用python內(nèi)置csv寫(xiě)入.csv文件。
import csv
with open('data.csv', 'w')as file:
dtwt = csv.writer(file)
dtwt.writerow(['世', '間', '美', '好', '與', '你', '環(huán)環(huán)', '相', '扣'])
import csv
with open('data.csv', 'r') as f:
data = csv.reader(f)
print(next(data))
Json文件
xml文件
路徑操作
Random包
生成隨機(jī)數(shù)
random.random()
**random.random()**作用是生成一個(gè)0到1之間的隨機(jī)數(shù),范圍包括0但不包括1,即 [0,1)。
random.random() Out[3]: 0.990545986753395
random.randint(start, end)
**random.randint(start,end)**作用是產(chǎn)生start到end的一個(gè)隨機(jī)整數(shù),要求start和end均為整數(shù)型。
random.randint(1,10) Out[4]: 3
random.uniform(start, end)
**random.uniform(start,end)**作用是產(chǎn)生start到end的一個(gè)隨機(jī)浮點(diǎn)數(shù),start和end不需要為整數(shù)型。
random.uniform(2.3,5) Out[5]: 4.370526664286709
元素取值
random.choice(seq)
** random.choice(seq)**作用是從序列seq中隨機(jī)選取一個(gè)元素。
alist = ['a',1,2] random.choice(alist) Out[7]: 2
random.sample(population,k)
** random.sample(population,k)**作用是從population序列中,隨機(jī)獲取k個(gè)元素,生成一個(gè)新序列。sample不改變?cè)瓉?lái)序列。
blist= [1,2,3,4,5] random.sample(blist,4) Out[11]: [4, 5, 2, 3] blist Out[12]: [1, 2, 3, 4, 5]
打亂序列
random.shuffle(x)
** random.shuffle(x)**作用是把序列x中的元素順序打亂。shuffle直接改變?cè)械男蛄小?/p>
clist = ['a','b','c','d'] random.shuffle(clist) clist Out[15]: ['d', 'a', 'c', 'b']
設(shè)置隨機(jī)種子
random.seed()
** random.seed()**的作用是改變隨機(jī)數(shù)生成器的種子,可以在調(diào)用其他隨機(jī)模塊函數(shù)之前調(diào)用此函數(shù), 注意其實(shí)是偽隨機(jī)數(shù),只要初始值一樣,得到的結(jié)果會(huì)是一樣的,在python中,默認(rèn)用系統(tǒng)時(shí)間作為seed。你也可以手動(dòng)調(diào)用random.seed(x)來(lái)指定seed。
random.seed(20) random.randint(1,10) Out[17]: 3 random.randint(1,10) Out[18]: 5 random.seed(20) random.randint(1,10) Out[20]: 3
到此這篇關(guān)于python代碼實(shí)現(xiàn)備忘錄案例講解的文章就介紹到這了,更多相關(guān)python代碼備忘錄內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信