这是我的代码:
old_stdout = sys.stdout
pymol.finish_launching()
log_file=open('loops.log', "w")
sys.stdout = log_file
def myfunc(resi):
print '%s' % (resi)
file= '%s'%model_initial
x= pymol.cmd.load (file, 'r')
myspace = {'myfunc': myfunc}
d= pymol.cmd.iterate('(ss l)', 'myfunc(resi)', space=myspace)
print d
pymol.cmd.quit()
sys.stdout = old_stdout
log_file.close()
old_stdout = sys.stdout
pymol.finish_launching()
log_file=open('list.log', "w")
sys.stdout = log_file
infile = 'loops.log'
outfile = 'loops1.log'
try:
delete_list = [ "Adjusting settings to improve performance for Intel cards."]
fin = open(infile)
fout = open(outfile, "w+")
for line in fin:
for word in delete_list:
line = line.replace(word, "")
fout.write(line)
fin.close()
fout.close()
outfile = infile
except:
infile = infile
with open(INFILE) as f:
lines = f.read().splitlines()
lines = lines
lines=lines[:-1]
lines=map(int, lines) #Convert str list to int list
lines=lines[:-1]
lines=map(int, lines) #Convert str list to int list
def group_runs(li,tolerance=2):
out = []
last = li[0]
for x in li:
if x-last > tolerance:
yield out
out = []
out.append(x)
last = x
yield out
print list(group_runs(lines))
sys.stdout = old_stdout
log_file.close()
old_stdout = sys.stdout
pymol.finish_launching()
log_file=open('list.log', "w")
sys.stdout = log_file
fhand=open('list.log')
for lines in fhand:
print lines
for k, g in groupby(enumerate(lines), lambda (i,x):i-x):
ranges = []
group = map(itemgetter(1), g)
ranges.append((group[0], group[-1]))
print ranges
sys.stdout = old_stdout
log_file.close()
文件loops.log,是一个包含许多数字的文件(选择模型初始化,并且该CODE的函数主体将它们分组为例如:
1
2
3
4
8
9
11
15
将他们分组:
(1,4)
(8,9)
(11,11)
(15,15)
这个CODE有效,但是,当文件有很多数字时,我有这样的信息:
Traceback (most recent call last):
File "./automaticvF.py", line 232, in <module>
lines=map(int, lines) #Convert str list to int list
ValueError: invalid literal for int() with base 10: 'Adjusting settings to improve performance for Intel cards.
并且日志是白色的。我认为更容易删除我的文件loops.log的消息,并更改文件的名称。只有当有很多很多的数字要分组时才会发生这种情况,但是当我有80个数字时,没问题。那么我可以在这个CODe中更改什么,以便使用变量infile,具体取决于消息是否在infile old?
有人可以帮帮我吗?
由于
答案 0 :(得分:0)
您不需要通过更改sys.stdout将流程的标准输出重定向到文件,而是需要直接将数据写入文件。你可以这样做,例如:
print >> log_file, '%s' % (resi)
或者像这样:
log_file.write(str(resi))
这将确保您的文件不会包含恰好打印到标准输出的随机无关数据。