Python uses a regular search string or a floating-point number code instance in a file _python

Source: Internet
Author: User
Tags numeric

Use Python and numpy to process data more frequently, writing several small functions to easily read and write data:

#-*-Coding:utf-8-*-#----------------------------------------------------------------------# FileName: gettxtdata.py #功能: Reading numeric data (floating point numbers) #主要提供类似matlab中的dlmread和dlmwrite函数 #同时提供loadtxtdata和savetxtdata函数 #Data in strings and files: 2013-1-10 #Author: Wu Xuping #----------------------------------------------------------------------Import NumPy #------- ---------------------------------------------------------------def stringtodoublearray (String): "" "#
  
  Replaces all non-double characters in a string with a space #以 ' # ' begins with a comment until the end of the line, and is emptied #返回一维numpy. Array "" "from Stringio import Stringio import re Dataarray=numpy.empty ([0],numpy.float64) If Len (String.strip ()) >0: #清空注释行, all with the ' # ' start character doublestring=re.s UB (' #.*$ ', "", String, Count=0, Flags=re.) IGNORECASE) #删除非数字字符 doublestring=re.sub (' [^0-9.e+-] ', "", Doublestring, Count=0, Flags=re. IGNORECASE) #去掉不正确的数字格式 (code duplication is necessary) 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 (double String) dataarray= Numpy.genfromtxt (striods) return DataArray #----------------------------------------------- -----------------------def getdoublelistfromstring (String): "" "#使用换行符分割字符串 #将字符串中的所有非Double类型的字符全部替换成空格 #以 ' # ' opening note Released to the end of the line, are emptied #将每一行转换成numpy. Array array #返回numpy the list "" "from Stringio import Stringio import re Doublelis T=[] Stringlist=string.split (' \ n ') #使用换行符分割字符串 for line in Stringlist:if Len (Line.strip ()) >0: #清空注释行, all with ' # ' opening character doublestring=re.sub (' #.*$ ', ' ", Line, Count=0, Flags=re. IGNORECASE) #删除非数字字符 doublestring=re.sub (' [^0-9.e+-] ', "", Doublestring, Count=0, Flags=re. IgnorecASE) #去掉不正确的数字格式 (code duplication is necessary) 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=stringi O (doublestring) doublelist.append (Numpy.genfromtxt (striods)) return Doublelist #-------------------------- --------------------------------------------def getdoublelistfromfile (FileName): "" "# Replaces all of the double characters in a text file with the Numpy.array array #每一行都是numpy. Array # #返回numpy a list of. Array #注意: Each element that returns a list is a Numpy.array array #注 Meaning: Each element of the return list (or file per line) can contain different numbers "" "File=open (FileName, ' r ') Read_file = File.read () file.close () doublelist=g Etdoublelistfromstring (read_file) return DoubLelist def dlmread (filename,dtype=numpy.float64): "" "#Load Data from Txt-file. #分隔符默认是: ";", ",", space class (including \ t) and so on #以 # is considered to be a comment and will not be read #Return value: Two-dimensional numeric array (Numpy.ndarray) #对文本中数据的排列格式要求最低, and allow annotation characters to appear, intelligent process The highest, but slower "" "Doublelist=getdoublelistfromfile (FileName) dlsize=[] #每一行数组的大小 for DL in DoubleList:dlsize.appen D (dl.size) mincolumnsize=min (dlsize) #数组的最大列数 Maxcolumnsize=max (dlsize) #数组的最小列数 #数组创建和赋值 doublearray=numpy.emp Ty ([Len (doublelist), Mincolumnsize],dtype=dtype) Row=range (0,len (doublelist)) Colum=range (0,mincolumnsize) for I in Row:for J. Colum:doublearray[i][j]=doublelist[i][j] return Doublearray #----------------------- -----------------------------------------------def loadtxtdata (filename,delimiter= ""): "" #Load Data from Txt-file
  With delimiter. #分隔符默认是: ";", ",", space class (including \ t) and custom delimiter #Return value: Two-dimensional numeric array (Numpy.ndarray) #对文本中数据的排列格式要求较高, and no annotation characters are allowed, the degree of intelligence is low,
But faster "" "from Stringio import Stringio  Import re file_handle=open (filename, ' R ') linesall=file_handle.read () #读入字符串 file_handle.close () delimiteral L=delimiter+ ",;" #分隔符 spacestring= "" #空格 for Rchar in Delimiterall:linesall=linesall.replace (rchar,spacestring) return nump Y.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)

I have only tried it in the python2.7, and if I want to use it in python3.x, I can test it myself.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.