Python USES regular searches for instances of floating point code in strings or files


With python and numpy to deal with data more times, wrote a few small functions, you can easily read and write data:

# -*- coding: utf-8 -*-
#----------------------------------------------------------------------
# FileName:gettxtdata.py
# function : Reads numeric data in strings and files ( Floating point Numbers )
# Mainly provide similar matlab In the dlmread and dlmwrite function
# At the same time provide loadtxtdata and savetxtdata function
#Data: 2013-1-10
#Author: Wu Xu flat
#----------------------------------------------------------------------
import numpy
#----------------------------------------------------------------------
def StringToDoubleArray(String):
  """
  # All non - string Double Type characters are all replaced with Spaces
  # In order to '#' Comment from the beginning to the end of the line , Have been cleared
  # Returns a d numpy.array An array of

  """
  from StringIO import StringIO
  import re

  DataArray=numpy.empty([0],numpy.float64)

  if len(String.strip())>0:
    # Empty comment lines , Are based on '#' Opening character
    doublestring=re.sub('#.*$', " ", String, count=0, flags=re.IGNORECASE)
    # Delete non-numeric characters
    doublestring=re.sub('[^0-9.e+-]', " ", doublestring, count=0, flags=re.IGNORECASE)
    # Remove incorrect number formatting ( 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)
    # Remove Spaces
    doublestring=doublestring.strip()
    if len(doublestring)>0:
      StrIOds=StringIO(doublestring)
      DataArray= numpy.genfromtxt(StrIOds)

  return DataArray

#----------------------------------------------------------------------
def GetDoubleListFromString(String):
  """
  # Split the string with a newline character
  # All non - string Double Type characters are all replaced with Spaces
  # In order to '#' Comment from the beginning to the end of the line , Have been cleared
  # Convert each row to numpy.array An array of
  # return numpy.array List of arrays

  """
  from StringIO import StringIO
  import re

  DoubleList=[]
  StringList=String.split('n')# Split the string with a newline character
  for Line in StringList:
    if len(Line.strip())>0:
      # Empty comment lines , Are based on '#' Opening character
      doublestring=re.sub('#.*$', " ", Line, count=0, flags=re.IGNORECASE)
      # Delete non-numeric characters
      doublestring=re.sub('[^0-9.e+-]', " ", doublestring, count=0, flags=re.IGNORECASE)
      # Remove incorrect number formatting ( 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)
      # Remove Spaces
      doublestring=doublestring.strip()
      if len(doublestring)>0:
        StrIOds=StringIO(doublestring)
        DoubleList.append(numpy.genfromtxt(StrIOds))
  return DoubleList

#----------------------------------------------------------------------
def GetDoubleListFromFile(FileName):
  """
  # Will text all in the file Double Type characters are all replaced with numpy.array An array of
  # Every row numpy.array An array of
  ## return numpy.array List of arrays
  # Pay attention to : Each element in the returned list is one again numpy.array An array of
  # Pay attention to : Returns each element of the list ( Or file per line ) It can contain Numbers of different Numbers

  """
  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.
  # The delimiter defaults to :";",",", Space class  ( including t) , etc.
  # In order to # The beginning is considered a comment , Will not be read
  #Return Value: Two dimensional array of values (numpy.ndarray)
  # Minimum requirement for arrangement of data in text , Comment characters are allowed , The highest degree of intelligence , But it's slower
  """
  DoubleList=GetDoubleListFromFile(FileName)
  dlsize=[]# The size of each row of the array
  for dL in DoubleList:
    dlsize.append(dL.size)

  MinColumnSize=min(dlsize)# The maximum number of columns in an array
  MaxColumnSize=max(dlsize)# The minimum number of columns in an array
  # Array creation and assignment
  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.
  # The delimiter defaults to :";",",", Space class  ( including t) And custom delimiter Etc.
  #Return Value:   Two dimensional array of values (numpy.ndarray)
  # The arrangement format of the data in the text is highly required , Comment characters are not allowed , The intelligence degree is low , But it's faster
  """
  from StringIO import StringIO
  import re

  file_handle=open(filename,'r')
  LinesALL=file_handle.read()# Read in string
  file_handle.close()

  DelimiterALL=delimiter+",;"# The separator
  SpaceString=" "# The blank space
  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

#----------------------------------------------------------------------
# The test program
#----------------------------------------------------------------------
if __name__ == '__main__':
  # Generate random number
  data=numpy.random.randn(3,4)
  filename='D:/x.txt'
  # Written to the file
  dlmwrite(filename,data)
  x=GetDoubleListFromFile(filename)
  print(x)
  print(dlmread(filename))
  y=StringToDoubleArray('79l890joj')
  print(y)
  z=loadtxtdata(filename)
  print(z)

I’ve only tried it in python2.7, and if you want to use it in python3.x, you can test it yourself.