python代碼實(shí)現(xiàn)備忘錄案例講解
文件操作
TXT文件
讀取txt文件
讀取txt文件全部內(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)容寫入txt文件。
直接寫入
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
按行寫入。
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']
寫入csv文件
使用python內(nèi)置csv寫入.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()**作用是生成一個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的一個隨機(jī)整數(shù),要求start和end均為整數(shù)型。
random.randint(1,10) Out[4]: 3
random.uniform(start, end)
**random.uniform(start,end)**作用是產(chǎn)生start到end的一個隨機(jī)浮點(diǎn)數(shù),start和end不需要為整數(shù)型。
random.uniform(2.3,5) Out[5]: 4.370526664286709
元素取值
random.choice(seq)
** random.choice(seq)**作用是從序列seq中隨機(jī)選取一個元素。
alist = ['a',1,2] random.choice(alist) Out[7]: 2
random.sample(population,k)
** random.sample(population,k)**作用是從population序列中,隨機(jī)獲取k個元素,生成一個新序列。sample不改變原來序列。
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直接改變原有的序列。
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é)果會是一樣的,在python中,默認(rèn)用系統(tǒng)時間作為seed。你也可以手動調(diào)用random.seed(x)來指定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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python+Selenium實(shí)現(xiàn)無頭瀏覽器網(wǎng)頁截圖
這篇文章主要為大家詳細(xì)介紹了Python+Selenium實(shí)現(xiàn)無頭瀏覽器網(wǎng)頁截圖的相關(guān)知識,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-03-03
python中l(wèi)ogging庫的使用總結(jié)
Python的logging模塊提供了通用的日志系統(tǒng),可以方便第三方模塊或者是應(yīng)用使用,下面這篇文章主要給大家介紹了關(guān)于python中l(wèi)ogging庫使用的一些知識總結(jié),文中給出了詳細(xì)的示例代碼,需要的朋友可以參考借鑒,下面來一起看看吧。2017-10-10
python繪制散點(diǎn)圖詳細(xì)步驟(從0到1必會)
這篇文章主要介紹了如何使用Python繪制散點(diǎn)圖,包括導(dǎo)入包、準(zhǔn)備數(shù)據(jù)、繪制圖像、修飾圖像(添加標(biāo)題、坐標(biāo)軸標(biāo)簽、顏色圖例)以及整合所有代碼,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-12-12
Python numpy大矩陣運(yùn)算內(nèi)存不足如何解決
這篇文章主要介紹了Python numpy大矩陣運(yùn)算內(nèi)存不足如何解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-11-11
python sorted函數(shù)原理解析及練習(xí)
這篇文章主要介紹了python sorted函數(shù)原理解析及練習(xí),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-02-02
利用Python實(shí)現(xiàn)外觀數(shù)列求解
這篇文章主要介紹了利用Python實(shí)現(xiàn)外觀數(shù)列求解,文章利用舉例說明文章的主題內(nèi)容,具有一定的參考價值,需要的小伙伴樂意參考一下2022-03-03
詳解pandas映射與數(shù)據(jù)轉(zhuǎn)換
這篇文章主要介紹了pandas映射與數(shù)據(jù)轉(zhuǎn)換的相關(guān)資料,幫助大家更好的利用python進(jìn)行數(shù)據(jù)分析,感興趣的朋友可以了解下2021-01-01

