python代碼實現(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包
生成隨機數(shù)
random.random()
**random.random()**作用是生成一個0到1之間的隨機數(shù),范圍包括0但不包括1,即 [0,1)。
random.random() Out[3]: 0.990545986753395
random.randint(start, end)
**random.randint(start,end)**作用是產(chǎn)生start到end的一個隨機整數(shù),要求start和end均為整數(shù)型。
random.randint(1,10) Out[4]: 3
random.uniform(start, end)
**random.uniform(start,end)**作用是產(chǎn)生start到end的一個隨機浮點數(shù),start和end不需要為整數(shù)型。
random.uniform(2.3,5) Out[5]: 4.370526664286709
元素取值
random.choice(seq)
** random.choice(seq)**作用是從序列seq中隨機選取一個元素。
alist = ['a',1,2] random.choice(alist) Out[7]: 2
random.sample(population,k)
** random.sample(population,k)**作用是從population序列中,隨機獲取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è)置隨機種子
random.seed()
** random.seed()**的作用是改變隨機數(shù)生成器的種子,可以在調(diào)用其他隨機模塊函數(shù)之前調(diào)用此函數(shù), 注意其實是偽隨機數(shù),只要初始值一樣,得到的結(jié)果會是一樣的,在python中,默認用系統(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代碼實現(xiàn)備忘錄案例講解的文章就介紹到這了,更多相關(guān)python代碼備忘錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Python+Pyecharts實現(xiàn)漏斗圖的繪制
漏斗圖是一個簡單的散點圖,反映研究在一定樣本量或精確性下單個研究的干預(yù)效應(yīng)估計值。本文將用Python Pyecharts實現(xiàn)漏斗圖的繪制,需要的可以參考一下2022-06-06
Python編程django實現(xiàn)同一個ip十分鐘內(nèi)只能注冊一次
這篇文章主要介紹了Python編程django實現(xiàn)同一個ip十分鐘內(nèi)只能注冊一次的相關(guān)內(nèi)容,具有一定參考價值。需要的朋友可以了解下。2017-11-11
TensorFlow實現(xiàn)數(shù)據(jù)增強的示例代碼
?TensorFlow數(shù)據(jù)增強?是一種通過變換和擴充訓(xùn)練數(shù)據(jù)的方法,本文主要介紹了TensorFlow實現(xiàn)數(shù)據(jù)增強的示例代碼,具有一定的參考價值,感興趣的可以了解游戲2024-08-08

