gpt4 book ai didi

python - 循环遍历 Names 并在 Name 中添加 1(如果已找到)

转载 作者:太空宇宙 更新时间:2023-11-03 15:21:34 28 4
gpt4 key购买 nike

这是 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 session 中)。

此外,它创建了 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()

关于python - 循环遍历 Names 并在 Name 中添加 1(如果已找到),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43498273/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com