python使用正則搜索字符串或文件中的浮點(diǎn)數(shù)代碼實(shí)例
更新時(shí)間:2014年07月11日 09:05:48 投稿:junjie
這篇文章主要介紹了python使用正則搜索字符串或文件中的浮點(diǎn)數(shù)代碼實(shí)例,同時(shí)包含一個(gè)讀寫到文件功能,需要的朋友可以參考下
用python和numpy處理數(shù)據(jù)次數(shù)比較多,寫了幾個(gè)小函數(shù),可以方便地讀寫數(shù)據(jù):
# -*- coding: utf-8 -*- #---------------------------------------------------------------------- # FileName:gettxtdata.py #功能:讀取字符串和文件中的數(shù)值數(shù)據(jù)(浮點(diǎn)數(shù)) #主要提供類似matlab中的dlmread和dlmwrite函數(shù) #同時(shí)提供loadtxtdata和savetxtdata函數(shù) #Data: 2013-1-10 #Author:吳徐平 #---------------------------------------------------------------------- import numpy #---------------------------------------------------------------------- def StringToDoubleArray(String): """ #將字符串中的所有非Double類型的字符全部替換成空格 #以'#'開頭注釋直至行尾,都被清空 #返回一維numpy.array數(shù)組 """ from StringIO import StringIO import re DataArray=numpy.empty([0],numpy.float64) if len(String.strip())>0: #清空注釋行,都是以'#'開頭子字符 doublestring=re.sub('#.*$', " ", String, count=0, flags=re.IGNORECASE) #刪除非數(shù)字字符 doublestring=re.sub('[^0-9.e+-]', " ", doublestring, count=0, flags=re.IGNORECASE) #去掉不正確的數(shù)字格式(代碼重復(fù)是有必要的) doublestring=re.sub('[.e+-](?=\s)', " ", doublestring, count=0, flags=re.IGNORECASE) doublestring=re.sub('[.e+-](?=\s)', " ", doublestring, count=0, flags=re.IGNORECASE) doublestring=re.sub('[e+-]$', " ", doublestring, count=0, flags=re.IGNORECASE) doublestring=re.sub('[e+-]$', " ", doublestring, count=0, flags=re.IGNORECASE) #去掉首尾空格 doublestring=doublestring.strip() if len(doublestring)>0: StrIOds=StringIO(doublestring) DataArray= numpy.genfromtxt(StrIOds) return DataArray #---------------------------------------------------------------------- def GetDoubleListFromString(String): """ #使用換行符分割字符串 #將字符串中的所有非Double類型的字符全部替換成空格 #以'#'開頭注釋直至行尾,都被清空 #將每一行轉(zhuǎn)換成numpy.array數(shù)組 #返回numpy.array數(shù)組的列表 """ from StringIO import StringIO import re DoubleList=[] StringList=String.split('\n')#使用換行符分割字符串 for Line in StringList: if len(Line.strip())>0: #清空注釋行,都是以'#'開頭子字符 doublestring=re.sub('#.*$', " ", Line, count=0, flags=re.IGNORECASE) #刪除非數(shù)字字符 doublestring=re.sub('[^0-9.e+-]', " ", doublestring, count=0, flags=re.IGNORECASE) #去掉不正確的數(shù)字格式(代碼重復(fù)是有必要的) doublestring=re.sub('[.e+-](?=\s)', " ", doublestring, count=0, flags=re.IGNORECASE) doublestring=re.sub('[.e+-](?=\s)', " ", doublestring, count=0, flags=re.IGNORECASE) doublestring=re.sub('[e+-]$', " ", doublestring, count=0, flags=re.IGNORECASE) doublestring=re.sub('[e+-]$', " ", doublestring, count=0, flags=re.IGNORECASE) #去掉首尾空格 doublestring=doublestring.strip() if len(doublestring)>0: StrIOds=StringIO(doublestring) DoubleList.append(numpy.genfromtxt(StrIOds)) return DoubleList #---------------------------------------------------------------------- def GetDoubleListFromFile(FileName): """ #將文本文件中的所有Double類型的字符全部替換成numpy.array數(shù)組 #每一行都是numpy.array數(shù)組 ##返回numpy.array數(shù)組的列表 #注意:返回列表的每個(gè)元素又都是一個(gè)numpy.array數(shù)組 #注意:返回列表的每個(gè)元素(或文件每行)可以包含不同多個(gè)數(shù)的數(shù)字 """ file=open(FileName, 'r') read_file = file.read() file.close() DoubleList=GetDoubleListFromString(read_file) return DoubleList def dlmread(FileName,dtype=numpy.float64): """ #Load Data From Txt-File. #分隔符默認(rèn)是:";",",",空格類 (包括\t)等等 #以#開頭的被認(rèn)為是注釋,不會被讀取 #Return Value:二維數(shù)值數(shù)組(numpy.ndarray) #對文本中數(shù)據(jù)的排列格式要求最低,且容許出現(xiàn)注釋字符,智能化程度最高,但速度較慢 """ DoubleList=GetDoubleListFromFile(FileName) dlsize=[]#每一行數(shù)組的大小 for dL in DoubleList: dlsize.append(dL.size) MinColumnSize=min(dlsize)#數(shù)組的最大列數(shù) MaxColumnSize=max(dlsize)#數(shù)組的最小列數(shù) #數(shù)組創(chuàng)建和賦值 DoubleArray=numpy.empty([len(DoubleList),MinColumnSize],dtype=dtype) row=range(0,len(DoubleList)) colum=range(0,MinColumnSize) for i in row: for j in colum: DoubleArray[i][j]=DoubleList[i][j] return DoubleArray #---------------------------------------------------------------------- def loadtxtdata(filename,delimiter=""): """ #Load Data From Txt-File with delimiter. #分隔符默認(rèn)是:";",",",空格類 (包括\t)和自定義的delimiter等 #Return Value: 二維數(shù)值數(shù)組(numpy.ndarray) #對文本中數(shù)據(jù)的排列格式要求較高,且不容許出現(xiàn)注釋字符,智能化程度較低,但速度較快 """ from StringIO import StringIO import re file_handle=open(filename,'r') LinesALL=file_handle.read()#讀入字符串 file_handle.close() DelimiterALL=delimiter+",;"#分隔符 SpaceString=" "#空格 for RChar in DelimiterALL: LinesALL=LinesALL.replace(RChar,SpaceString) return numpy.genfromtxt(StringIO(LinesALL)) #---------------------------------------------------------------------- def savetxtdata(filename, X, fmt='%.8e', delimiter=' ', newline='\n'): """ Save Data To Txt-File. """ numpy.savetxt(filename, X, fmt=fmt, delimiter=delimiter, newline=newline) return True #---------------------------------------------------------------------- def dlmwrite(filename, X, fmt='%.8e', delimiter=' ', newline='\n'): """ Save Data To Txt-File. """ numpy.savetxt(filename, X, fmt=fmt, delimiter=delimiter, newline=newline) return True #---------------------------------------------------------------------- #測試程序 #---------------------------------------------------------------------- if __name__ == '__main__': #生成隨機(jī)數(shù) data=numpy.random.randn(3,4) filename='D:/x.txt' #寫入文件 dlmwrite(filename,data) x=GetDoubleListFromFile(filename) print(x) print(dlmread(filename)) y=StringToDoubleArray('79l890joj') print(y) z=loadtxtdata(filename) print(z)
我只在python2.7中試過,如果要在python3.x中使用,可自行測試.
您可能感興趣的文章:
相關(guān)文章
Python識別快遞條形碼及Tesseract-OCR使用詳解
這篇文章主要介紹了Python識別快遞條形碼及Tesseract-OCR使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07Python實(shí)現(xiàn)將多個(gè)空格換為一個(gè)空格.md的方法
今天小編就為大家分享一篇Python實(shí)現(xiàn)將多個(gè)空格換為一個(gè)空格.md的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12python使用 zip 同時(shí)迭代多個(gè)序列示例
這篇文章主要介紹了python使用 zip 同時(shí)迭代多個(gè)序列,結(jié)合實(shí)例形式分析了Python使用zip遍歷迭代長度相等與不等的序列相關(guān)操作技巧,需要的朋友可以參考下2019-07-07使用Python簡單編寫一個(gè)股票監(jiān)控系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了如何使用Python簡單編寫一個(gè)股票監(jiān)控系統(tǒng),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-12-12