繁体   English   中英

如何将每个循环结果打印到单个文件?

[英]How to print each loop result to a single file?

我正在为Modeller运行模型评估协议。 它评估每个模型并将其结果写入单独的文件。 但是我必须为每个模型运行它并写入单个文件。

这是原始代码:

from modeller import *
from modeller.scripts import complete_pdb

log.verbose()    # request verbose output
env = environ()
env.libs.topology.read(file='$(LIB)/top_heav.lib') # read topology
env.libs.parameters.read(file='$(LIB)/par.lib') # read parameters

# read model file
mdl = complete_pdb(env, 'TvLDH.B99990001.pdb')

# Assess all atoms with DOPE:
s = selection(mdl)
s.assess_dope(output='ENERGY_PROFILE NO_REPORT', file='TvLDH.profile',
              normalize_profile=True, smoothing_window=15)

我添加了一个循环来评估单个运行中的每个模型,但是我创建了几个文件(每个模型一个),我想要在一个文件中打印所有评估

from modeller import *
from modeller.scripts import complete_pdb

log.verbose()    # request verbose output
env = environ()
env.libs.topology.read(file='$(LIB)/top_heav.lib') # read topology
env.libs.parameters.read(file='$(LIB)/par.lib') # read parameters

#My loop starts here
for i in range (1,1001):
    number=str(i)
    if i<10:
        name='000'+number
    else:
        if i<100:
           name='00'+number
        else:
             if i<1000:
                name='0'+number
             else:
                  name='1000'

# read model file
mdl = complete_pdb(env, 'TcP5CDH.B9999'+name+'.pdb')

# Assess all atoms with DOPE: this is the assesment that i want to print in the same file
s = selection(mdl)
savename='TcP5CDH.B9999'+name+'.profile'
s.assess_dope(output='ENERGY_PROFILE NO_REPORT', 
              file=savename,
              normalize_profile=True, smoothing_window=15)

由于我是编程新手,所以任何帮助都会非常有用!

欢迎:-)看起来你很亲密。 我们将向您介绍如何使用python函数和.format()语句。

你的原文有一个注释行# read model file ,看起来它可能是一个函数,所以让我们试试吧。 它可能看起来像这样。

from modeller import *
from modeller.scripts import complete_pdb

log.verbose()    # request verbose output
# I'm assuming this can be done just once
# and re-used for all your model files...
# (if not, the env stuff should go inside the
# read_model_file() function.
env = environ()
env.libs.topology.read(file='$(LIB)/top_heav.lib') # read topology
env.libs.parameters.read(file='$(LIB)/par.lib') # read parameters

def read_model_file(file_name):
    print('--- read_model_file(file_name='+file_name+') ---')
    mdl = complete_pdb(env, file_name)
    # Assess all atoms with DOPE:
    s = selection(mdl)
    output_file = file_name+'.profile'
    s.assess_dope(
        output='ENERGY_PROFILE NO_REPORT',
        file=output_file,
        normalize_profile=True,
        smoothing_window=15)

for i in range(1,1001):
    file_name = 'TcP5CDH.B9999{:04d}pdb'.format(i)
    read_model_file(file_name)

使用.format()我们可以获得多个if语句检查10? 100? 1000? 基本上.format()用参数替换{}花括号。 它可能相当复杂,但你不需要尽可能地解决所有问题。

示例: 'Hello {}!'.format('world')产生Hello world! {:04d}东西使用格式化,基本上是说“请制作一个4字符宽的数字子字符串并对其进行零填充,因此你应该得到'0001', ..., '0999', '1000'{:4d} (没有前导零)会给你空间填充结果(例如' 1', ..., ' 999', '1000' 。这里有关于零填充的更多信息: 带前导零的显示数字

暂无
暂无

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

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