python使用正則搜尋字串或檔案中的浮點數代碼執行個體

來源:互聯網
上載者:User
用python和numpy處理資料次數比較多,寫了幾個小函數,可以方便地讀寫資料:

# -*- coding: utf-8 -*-#----------------------------------------------------------------------# FileName:gettxtdata.py#功能:讀取字串和檔案中的數值資料(浮點數)#主要提供類似matlab中的dlmread和dlmwrite函數#同時提供loadtxtdata和savetxtdata函數#Data: 2013-1-10#Author:吳徐平#----------------------------------------------------------------------import numpy#----------------------------------------------------------------------def StringToDoubleArray(String):  """  #將字串中的所有非Double類型的字元全部替換成空格  #以'#'開頭注釋直至行尾,都被清空  #返回一維numpy.array數組  """   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)    #刪除非數字字元          doublestring=re.sub('[^0-9.e+-]', " ", doublestring, count=0, flags=re.IGNORECASE)    #去掉不正確的數字格式(代碼重複是有必要的)    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類型的字元全部替換成空格  #以'#'開頭注釋直至行尾,都被清空  #將每一行轉換成numpy.array數組  #返回numpy.array數組的列表  """   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)      #刪除非數字字元            doublestring=re.sub('[^0-9.e+-]', " ", doublestring, count=0, flags=re.IGNORECASE)      #去掉不正確的數字格式(代碼重複是有必要的)      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數組  #每一行都是numpy.array數組  ##返回numpy.array數組的列表  #注意:返回列表的每個元素又都是一個numpy.array數組  #注意:返回列表的每個元素(或檔案每行)可以包含不同多個數的數字  """   file=open(FileName, 'r')  read_file = file.read()  file.close()   DoubleList=GetDoubleListFromString(read_file)  return DoubleListdef dlmread(FileName,dtype=numpy.float64):  """  #Load Data From Txt-File.  #分隔字元預設是:";",",",空格類 (包括\t)等等  #以#開頭的被認為是注釋,不會被讀取  #Return Value:二維數值數組(numpy.ndarray)  #對文本中資料的排列格式要求最低,且容許出現注釋字元,智能化程度最高,但速度較慢  """  DoubleList=GetDoubleListFromFile(FileName)  dlsize=[]#每一行數組的大小  for dL in DoubleList:    dlsize.append(dL.size)      MinColumnSize=min(dlsize)#數組的最大列數  MaxColumnSize=max(dlsize)#數組的最小列數  #數組建立和賦值  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.  #分隔字元預設是:";",",",空格類 (包括\t)和自訂的delimiter等  #Return Value:  二維數值數組(numpy.ndarray)  #對文本中資料的排列格式要求較高,且不容許出現注釋字元,智能化程度較低,但速度較快  """  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__':  #產生隨機數  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中使用,可自行測試.

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.