聊一聊python常用的編程模塊
文件流的讀寫
讀取保存數(shù)據(jù)為數(shù)組的txt文件
使用try進(jìn)行異常發(fā)現(xiàn),使用while檢測(cè)文件末尾進(jìn)行讀取
file_to_read = raw_input("Enter file name of tests (empty string to end program):")
try:
infile = open(file_to_read, 'r')
while file_to_read != " ":
file_to_write = raw_input("Enter output file name (.csv will be appended to it):")
file_to_write = file_to_write + ".csv"
outfile = open(file_to_write, "w")
readings = (infile.readline())
print readings
while readings != 0:
global count
readings = int(readings)
minimum = (infile.readline())
maximum = (infile.readline())
使用for遍歷讀取的每一行,進(jìn)行一次性的讀取和輸入
下面調(diào)用的程序讀取的數(shù)據(jù)是

result = list()
with open('../test/parameter.txt') as f:
for line in f.readlines():
temp = list()
# 逐個(gè)遍歷對(duì)應(yīng)每一行元素,將之轉(zhuǎn)為對(duì)應(yīng)的數(shù)據(jù)
b = line.strip(",][").split(',')
if(len(b) >= 5):
b.pop()
for a in b:
a = a.replace('[','').replace(']','')
temp.append(float(a))
result.append(temp)
#print("中途打印的temp是",temp)
#print("加入到result中的結(jié)果是",result)
刪除str中的特定字符
刪除字符串首尾的多余字符串strip()
# 刪除字符串中多余字符
def string_remove():
str1 = ' abc \n'
print str1.strip() # abc
str2 = '----abcdf++++'
print str2.strip('-+') # abcdf
replace函數(shù),刪除字符串中某一個(gè)所有的字符串
ss = 'old old string'
ret = ss.replace('old', 'new', 1)
print(ret)
sub函數(shù),同時(shí)刪除多個(gè)字符串,這里使用了正則表達(dá)式
str2 = '\nabc\nwrt22\t666\t' # 刪除字符串中的所有\(zhòng)n,\t
import re
print(re.sub('[\n\t]','',str2)) # abcwrt22666
以上就是聊一聊python常用的編程模塊的詳細(xì)內(nèi)容,更多關(guān)于python編程模塊的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python topk()函數(shù)求最大和最小值實(shí)例
這篇文章主要介紹了python topk()函數(shù)求最大和最小值實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
Python 實(shí)現(xiàn)日志同時(shí)輸出到屏幕和文件
這篇文章主要介紹了Python 實(shí)現(xiàn)日志同時(shí)輸出到屏幕和文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02
Python實(shí)現(xiàn)的搖骰子猜大小功能小游戲示例
這篇文章主要介紹了Python實(shí)現(xiàn)的搖骰子猜大小功能小游戲,涉及Python隨機(jī)數(shù)運(yùn)算與數(shù)值判斷相關(guān)操作技巧,需要的朋友可以參考下2017-12-12
對(duì)python 數(shù)據(jù)處理中的LabelEncoder 和 OneHotEncoder詳解
今天小編就為大家分享一篇對(duì)python 數(shù)據(jù)處理中的LabelEncoder 和 OneHotEncoder詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Python實(shí)現(xiàn)讀取txt文件中的數(shù)據(jù)并繪制出圖形操作示例
這篇文章主要介紹了Python實(shí)現(xiàn)讀取txt文件中的數(shù)據(jù)并繪制出圖形操作,涉及Python文件讀取、數(shù)值運(yùn)算及基于pylab庫(kù)的圖形繪制相關(guān)操作技巧,需要的朋友可以參考下2019-02-02
python實(shí)現(xiàn)將視頻按幀讀取到自定義目錄
今天小編就為大家分享一篇python實(shí)現(xiàn)將視頻按幀讀取到自定義目錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
Python動(dòng)態(tài)聲明變量賦值代碼實(shí)例
這篇文章主要介紹了Python動(dòng)態(tài)聲明變量賦值代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
Python調(diào)用Zoomeye搜索接口的實(shí)現(xiàn)
本文主要介紹了Python調(diào)用Zoomeye搜索接口的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01

