📄 fileio.py
字号:
"""Input and output flat ASCII files
$Id: fileIO.py Copyright (C) 2005 Roger Jarvis
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""
import string, scipy
import sys
def load(filename,delim='\t'):
"""for importing delimited ASCII files
>>> import os
>>> l = load(os.path.join("examples","FT-IR urinary tract infection bacterial fingerprints","rawdata.txt"))
"""
f = file(filename,'r')
lines = f.readlines()
f.close()
c = 0
for row in lines:
myline = []
splitrow = row.strip().split(delim)
for item in splitrow:
myline.append(float(item))
if c == 0:
lines = scipy.reshape(myline,(1,len(splitrow)))
c += 1
else:
lines = scipy.concatenate((lines,scipy.reshape(myline,(1,len(splitrow)))),0)
return lines
def save(myarray,filename):
"""Export array to a tab delimited file
"""
size = myarray.shape
f = file(filename,'w')
for line in myarray:
c = 0
for item in line:
if c == size[1]-1:
f.write(`item`)
else:
f.write(`item` + '\t')
c = c+1
f.write('\n')
f.close()
##def load_manova(filename,wdir):
## """Import MANOVA results from Minitab
## """
## f=file(string.join((wdir,'/',filename),''),'r')
## t=f.readlines()
## f.close()
## out={}
## ind,do = 0,0
## for line in t:
## if line[0:4] == 'SSCP':
## fill=str(string.split(line,'\n')[0])
## out[fill] = []
##
## count = ind+3
## if do == 0:
## complex,rows,do = 0,0,1
## while count < len(t):
## if t[count-1] == '\n' and string.split(t[count],' ')[0] == '':
## if complex == 0:
## rows=count-(ind+4)
## complex+=1
## elif count == len(t):
## break
## elif t[count] == '\n' and t[count+1] == '\n':
## break
## count+=1
##
## count=ind+3
## if rows == 0:
## while count < len(t):
## if t[count] != '\n':
## out[fill].append(t[count])
## else:
## break
## count+=1
## else:
## while count < ind+3+rows:
## if t[count] != '\n':
## for find in range(0,complex+1,1):
## out[fill].append(t[count+((rows+2)*find)])
## count+=1
## ind+=1
##
## for each in out.keys():
## temp = out[each]
## test = string.split(each,'*')
## each = ''
## for item in test:
## each = string.join((each,item),'')
## eachout = []
## for item in temp:
## tempstring=string.split(item,' ')
## del tempstring[0]
## for x in tempstring:
## if x != '':
## eachout.append(x)
## if len(eachout) != 0:
## newarray=reshape(asarray(float(eachout[0]),'Float64'),(1,))
## for i in range(1,len(eachout),1):
## newarray=concatenate((newarray,reshape(asarray(float(eachout[i]),'Float64'),(1,))),1)
## print string.join(('Exporting ',wdir,'/',each,'.txt'),'')
## save(reshape(newarray,(int(sqrt(len(newarray))),int(sqrt(len(newarray))))),string.join((wdir,'/',each,'.txt'),''))
if __name__=="__main__":
import fileIO,doctest
doctest.testmod(fileIO,verbose=True)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -