python使用正則搜索字符串或文件中的浮點數(shù)代碼實例
更新時間:2014年07月11日 09:05:48 投稿:junjie
這篇文章主要介紹了python使用正則搜索字符串或文件中的浮點數(shù)代碼實例,同時包含一個讀寫到文件功能,需要的朋友可以參考下
用python和numpy處理數(shù)據次數(shù)比較多,寫了幾個小函數(shù),可以方便地讀寫數(shù)據:
# -*- coding: utf-8 -*-
#----------------------------------------------------------------------
# FileName:gettxtdata.py
#功能:讀取字符串和文件中的數(shù)值數(shù)據(浮點數(shù))
#主要提供類似matlab中的dlmread和dlmwrite函數(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ù)字格式(代碼重復是有必要的)
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數(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ù)字格式(代碼重復是有必要的)
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ù)組的列表
#注意:返回列表的每個元素又都是一個numpy.array數(shù)組
#注意:返回列表的每個元素(或文件每行)可以包含不同多個數(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.
#分隔符默認是:";",",",空格類 (包括\t)等等
#以#開頭的被認為是注釋,不會被讀取
#Return Value:二維數(shù)值數(shù)組(numpy.ndarray)
#對文本中數(shù)據的排列格式要求最低,且容許出現(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.
#分隔符默認是:";",",",空格類 (包括\t)和自定義的delimiter等
#Return Value: 二維數(shù)值數(shù)組(numpy.ndarray)
#對文本中數(shù)據的排列格式要求較高,且不容許出現(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__':
#生成隨機數(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中使用,可自行測試.
您可能感興趣的文章:
相關文章
轉換科學計數(shù)法的數(shù)值字符串為decimal類型的方法
今天小編就為大家分享一篇轉換科學計數(shù)法的數(shù)值字符串為decimal類型的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07
Python實現(xiàn)創(chuàng)建模塊的方法詳解
導入一個模塊,我們一般都會使用?import?關鍵字,但有些場景下?import?難以滿足我們的需要。所以除了?import?之外還有很多其它導入模塊的方式,下面就來介紹一下2022-07-07
Python+Dlib+Opencv實現(xiàn)人臉采集并表情判別功能的代碼
這篇文章主要介紹了Python+Dlib+Opencv實現(xiàn)人臉采集并表情判別,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
Python 實現(xiàn)Numpy中找出array中最大值所對應的行和列
今天小編就為大家分享一篇Python 實現(xiàn)Numpy中找出array中最大值所對應的行和列,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11

