numpyを使うとpythonでデータをメモリに取り込むことができます.
numpyを使用すると、pythonでデータをメモリに取り込むことができます.
# -*- coding: utf-8 -*-
#~ #----------------------------------------------------------------------
#~ module:wlab
#~ Filename:wloadsave.py
#~ Function :
#~ def str2num(LineString,comment='#'):
#~ def file2list(Filename,comment='#',SkipHeadLine=0):
#~ def str2list(String,comment='#',SkipHeadLine=0):
#~ def dlmread(Filename,comment='#',dtype=numpy.float64,SkipHeadLine=0):
#~ def dlmwrite(Filename, X, fmt='%16.8e',delimiter=' ', newline='
',header=''):
#~ :
#~ ( )
#~ matlab dlmread dlmwrite
#~ str2num,str2list
#~ Data: 2013-08-08,
#~ Author:
#~ Email:[email protected]
#~ #----------------------------------------------------------------------
import numpy
import scipy
#~ #----------------------------------------------------------------------
def str2num(LineString,comment='#'):
"""
#~ >>>NumArray=str2num(LineString,comment='#')
#~ Double
#~ '#' ,
#~ numpy.array
"""
from StringIO import StringIO
import re,numpy
NumArray=numpy.empty([0],numpy.float64)
NumStr=LineString.strip()
#~ ignore comment string
for cmt in comment:
CmtRe=cmt+'.*$'
NumStr=re.sub(CmtRe, " ", NumStr.strip(), count=0, flags=re.IGNORECASE)
#~ delete all non-number characters,replaced by blankspace.
NumStr=re.sub('[^0-9.e+-]', " ", NumStr, count=0, flags=re.IGNORECASE)
#~ Remove incorrect combining-characters for double type.
NumStr=re.sub('[.e+-](?=\s)', " ", NumStr.strip(), count=0, flags=re.IGNORECASE)
NumStr=re.sub('[.e+-](?=\s)', " ", NumStr.strip(), count=0, flags=re.IGNORECASE)
NumStr=re.sub('[e+-]$', " ", NumStr.strip(), count=0, flags=re.IGNORECASE)
NumStr=re.sub('[e+-]$', " ", NumStr.strip(), count=0, flags=re.IGNORECASE)
if len(NumStr.strip())>0:
StrIOds=StringIO(NumStr.strip())
NumArray= numpy.genfromtxt(StrIOds)
return NumArray
#~ #----------------------------------------------------------------------
def file2list(Filename,comment='#',SkipHeadLine=0):
"""
#~ >>>NumList=file2list(Filename,comment='#',SkipHeadLine=0)
#~
#~ Double
#~ '#' ,
#~ :SkipHeadLine
#~ numpy.array
#~ numpy.array
#~ :
"""
from StringIO import StringIO
import re,numpy
file=open(Filename, 'r')
String = file.read()
file.close()
NumList=[]
StringList=String.split('
')#
LineNum=0
for Line in StringList:
LineNum=LineNum+1
if (LineNum>SkipHeadLine):
NumArray=str2num(Line,comment)
if len(NumArray)>0:
NumList.append(NumArray)
return NumList
#~ #----------------------------------------------------------------------
def str2list(String,comment='#',SkipHeadLine=0):
"""
#~ >>>NumList=str2list(String,comment='#',SkipHeadLine=0):
#~
#~ Double
#~ '#' ,
#~ :SkipHeadLine
#~ numpy.array
#~ numpy.array
#~ :
"""
from StringIO import StringIO
import re,numpy
NumList=[]
StringList=String.split('
')#
LineNum=0
for Line in StringList:
LineNum=LineNum+1
if (LineNum>SkipHeadLine):
NumArray=str2num(Line,comment)
if len(NumArray)>0:
NumList.append(NumArray)
return NumList
#~ #----------------------------------------------------------------------
def dlmread(Filename,comment='#',dtype=numpy.float64,SkipHeadLine=0):
"""
#~ Load Data From Txt-File.
#~ >>>NumpyArray= dlmread(Filename,comment='#',dtype=numpy.float64,SkipHeadLine=0)
#~ :";",",", ( \t)
#~ # ,
#~ :SkipHeadLine
#~ Return Value: (numpy.ndarray)
#~ , , ,
#~ :
#~ , # ,
#~ >>> x=dlmread(Filename)
#~ #! ,
#~ >>> x=dlmread(Filename,comment='#!')
#~ ,
"""
import numpy
NumList=file2list(Filename,comment='#',SkipHeadLine=SkipHeadLine)
#~ NumList
if (len(NumList)>0):
dlsize=[]#
for dL in NumList:
dlsize.append(dL.size)
#~ dlsize
MinColumnSize=min(dlsize)#
MaxColumnSize=max(dlsize)#
#~
NumpyArray=numpy.empty([len(NumList),MinColumnSize],dtype=dtype)
row=range(0,len(NumList))
colum=range(0,MinColumnSize)
for i in row:
for j in colum:
NumpyArray[i][j]=NumList[i][j]
else:
NumpyArray=[]
return NumpyArray
#~ #----------------------------------------------------------------------
def dlmwrite(Filename, X, fmt='%16.8e',delimiter=' ', newline='
',header=''):
"""
#~ Save Data To Txt-File.
#~ >>> dlmwrite(Filename, X, fmt='%16.8e',delimiter=' ', newline='
',header=''):
"""
import numpy
numpy.savetxt(Filename, X, fmt=fmt, delimiter=delimiter, newline=newline,header=header)
return
#~ #----------------------------------------------------------------------
def tests():
x=numpy.random.randn(3,4)
print(x)
Filename='x.txt'
header='look at this file'
dlmwrite(Filename,x,header=header)
y=dlmread(Filename)
print(y)
return 'wlab tests finished!'
#~ #
if __name__ == '__main__':
x=numpy.random.randn(3,4)
print(x)
Filename='x.txt'
header='look at this file'
dlmwrite(Filename,x,header=header)
y=dlmread(Filename)
print(y)
上のdmreadとdmwrite関数は使いやすいです.