遍历 Names 并在 Name 中加 1,如果已经找到

Loop through Names and add 1 to Name, if already found

这是一个 Python 脚本的 Abaqus 应用程序,但仍然是一个不需要 Abaqus 知识的一般问题。

我不是程序员,缺乏这种必要的思维方式,所以非常感谢您的帮助。

我有一个数据库,其中包含名为 步骤名称 的内容。我想创建一个实体,这也是一个步骤(一个新步骤)。如果已经存在这样的名称,我希望脚本将 +1 添加到名称中。如果那个存在,我希望它添加另一个 +1 等等。

到目前为止没有什么重要的,只是导入:

# importing some libraries
from abaqus import *
from odbAccess import *
from abaqusConstants import *
import visualization
import fileinput
import os
from odbAccess import *
from textRepr import *

现在我们打开文件:

odb_path="analysis6.odb"
my_odb=session.openOdb(name=odb_path,readOnly=FALSE)
# based upon our knowledge of the database, will be automated later
lastStep="loading step"

让我们自动化一些名称来源,对于理解问题仍然不是那么重要:

NaseInstance = (my_odb.rootAssembly.instances.keys())
MojeInstance = ( NaseInstance[-1] )
CelaMojeInstance=my_odb.rootAssembly.instances[MojeInstance]

现在是关键部分:

# initial number = zero
MyStepNumber=0
# This is how we want our step to be called
MyStepName="Artifficial_Step"

# if such name is there already, add one
if MyStepName in my_odb.steps.keys():
    MyStepNumber=MyStepNumber+1
    MyStepName=MyStepName+str(MyStepNumber)
    print ( "it is there, now we will rather call it", MyStepName, "Now you have to reopen the odb to see it")

    # this is how we stuff data into this new step. Not important for this query
    artiffStep = my_odb.Step(name=MyStepName, description='postprocess operations', domain=TIME, timePeriod=1.0)
    artiffFrame = artiffStep.Frame(frameId=0, frameValue=0.0, description='ArtifFrame')
    sessionField = artiffFrame.FieldOutput(name='Field-1', description='NEW_FIELD', field=my_odb.steps[lastStep].frames[-1].fieldOutputs['S'])

    # save and close to make the changes work
    my_odb.save()
    my_odb.close()

else:
    # if such name doesnt exist yet, no problem to use it
    print ( "it is not there, no problem to call it", MyStepName, "Now you have to reopen the odb")

    # rest same as in the -if- statement
    artiffStep = my_odb.Step(name=MyStepName, description='postprocess operations', domain=TIME, timePeriod=1.0)
    artiffFrame = artiffStep.Frame(frameId=0, frameValue=0.0, description='ArtifFrame')
    sessionField = artiffFrame.FieldOutput(name='Field-1', description='NEW_FIELD', field=my_odb.steps[lastStep].frames[-1].fieldOutputs['S'])

    # save and close to make the changes work
    my_odb.save()
    my_odb.close()

不幸的是,这些具有任何编码知识的人可能会立即看到,如果尚未使用该名称,它将首次创建新步骤,并将其命名为 "Artifficial_Step"。如果它存在并且 else 语句开始起作用,它会说“......我们宁愿称它为 "Artifficial_Step1" 但随后不会创建任何新内容(可能因为这样的步骤已经存在)。而不是检查 Artifficial_Step1 是否也不存在,或者 Step2Step3Step4...

我想我需要一些循环来遍历名称 - my_odb.steps.keys() - 并不断添加一个,直到它到达一个不存在的名称以稍后创建它。我可以寻求帮助吗?

编辑: 现在试试这个:

MyStepNumber=int(0)
MyStepName="Artifficial_Step"

while MyStepName in my_odb.steps.keys():
    MyStepNumber=MyStepNumber+1
    MyStepName=MyStepName+str(MyStepNumber)
    print ( "it is there, now we will rather call it", MyStepName, "Now you have to reopen the odb")

    artiffStep = my_odb.Step(name=MyStepName, description='postprocess operations', domain=TIME, timePeriod=1.0)
    artiffFrame = artiffStep.Frame(frameId=0, frameValue=0.0, description='ArtifFrame')
    sessionField = artiffFrame.FieldOutput(name='Field-1', description='NEW_FIELD', field=my_odb.steps[lastStep].frames[-1].fieldOutputs['S'])

    # save and close to make the changes work
    my_odb.save()
    my_odb.close()

else:
    print ( "it is not there, no problem to call it", MyStepName, "Now you have to reopen the odb")

    artiffStep = my_odb.Step(name=MyStepName, description='postprocess operations', domain=TIME, timePeriod=1.0)
    artiffFrame = artiffStep.Frame(frameId=0, frameValue=0.0, description='ArtifFrame')
    sessionField = artiffFrame.FieldOutput(name='Field-1', description='NEW_FIELD', field=my_odb.steps[lastStep].frames[-1].fieldOutputs['S'])

    # save and close to make the changes work
    my_odb.save()
    my_odb.close()

但是 while 即使在关闭数据库后仍然继续,因此发出一条消息,表明数据库不再存在(意味着在当前 GUI 会话中)。

此外,它创建了 Artifficial_Step,然后在重新运行后创建了 Artifficial_Step1,然后(关闭并重新运行后)仅此而已。

# create a new step, frame, field. If it exists, add number:

MyStepNumber=int(1)
MyStepName="Artifficial_Step"+str(MyStepNumber)



while MyStepName in my_odb.steps.keys():
    MyStepNumber=MyStepNumber+1
    MyStepName="Artifficial_Step"+str(MyStepNumber)



artiffStep = my_odb.Step(name=MyStepName,      description='postprocess operations', domain=TIME, timePeriod=1.0)
artiffFrame = artiffStep.Frame(frameId=0, frameValue=0.0,     description='ArtifFrame')
sessionField = artiffFrame.FieldOutput(name='Field-1',     description='NEW_FIELD', field=my_odb.steps[lastStep].frames[-1].fieldOutputs['S'])

# save and close to make the changes work
my_odb.save()
my_odb.close()