简体   繁体   English

如何删除特定于文件的消息? 蟒蛇

[英]How can remove a message specific of a file? python

This is my CODE: 这是我的代码:

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()

the file loops.log, is a file with many numbers (select them of model initial, and the function principal of this CODE is group them for example: 文件loops.log,是一个包含许多数字的文件(选择模型初始化,并且此CODE的函数主体将它们分组,例如:

1 1

2 2

3 3

4 4

8 8

9 9

11 11

15 15

group them: 他们分组:

(1,4) (1,4)

(8,9) (8,9)

(11, 11) (11,11)

(15, 15) (15,15)

This CODE works, but, when file have many many numbers, I have this message : 这个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.

and the log is white. 并且日志是白色的。 I Think that is more easy remove the message of my file loops.log, and change name of file. 我认为更容易删除我的文件loops.log的消息,并更改文件的名称。 this only happens when have many many many numbers to group, but when I have 80 numbers for example, no problem. 只有当有很多很多的数字要分组时才会发生这种情况,但是当我有80个数字时,没问题。 so What can I change in this CODe, for use the variable infile, depending if the message is or not is in the infile old? 那么我可以在这个CODe中改变什么,使用变量infile,这取决于消息是否在infile old?

Can somebody help me please? 有人可以帮帮我吗?

Thanks 谢谢

Instead of redirecting the standard output of your process to a file by changing sys.stdout, you need to write your data to the file directly. 您无需通过更改sys.stdout将流程的标准输出重定向到文件,而是需要直接将数据写入文件。 You can do it, for example, like this: 你可以这样做,例如,像这样:

print >> log_file, '%s' % (resi)

Or like this: 或者像这样:

log_file.write(str(resi))

This will ensure that your file will not contain random unrelated data that happens to be printed to the standard output. 这将确保您的文件不包含恰好打印到标准输出的随机无关数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM