我仍然在用python复制和替换行,一个问题Here。基本上,我想统计一段中一个模式的数量,并在行中更新它。我想我已经在我的问题中发现了问题:我调用一个子函数来在主函数中交互同一个文件,而这个交互在某个时候是混乱的。我对编程还很陌生,我不知道如何用另一种方式来做这个复制统计来代替复制的事情。欢迎任何建议或提示。你知道吗
下面是我现在得到的部分代码:
# sum number of keyframes
def sumKeys (sceneObj, objName):
sceneObj.seek(0)
block = []
Keys = ""
for line in sceneObj:
if line.find("ObjectAlias " + objName + "\n") != -1:
for line in sceneObj:
if line.find("BeginKeyframe") != -1:
for line in sceneObj:
if line.find("default") != -1:
block.append(line.rstrip())
Keys = len(block)
elif line.find("EndKeyframe") != -1:
break
break
break
return (Keys)
# renew number of keyframes
def renewKeys (sceneObj, objName):
sceneObj.seek(0)
newscene = ""
item = []
for line in sceneObj:
newscene += line
for obj in objName:
if line.find("ObjectAlias " + obj + "\n") != -1:
for line in sceneObj:
if line.find("EndKeyframe") != -1:
newscene += line
break
if line.find("BeginKeyframe") != -1:
item = line.split()
newscene += item[0] + " " + str(sumKey(sceneObj, obj)) + " " + item[-1] + "\n"
continue
else:
newscene += line
return (newscene)
原件:
lines
BeginObjects
lines
ObjectAlias xxx
lines
BeginKeyframe 34 12 ----> 34 is what I want to replace
lines
EndObject
BeginAnotherObjects
...
目标:
lines
BeginObjects
lines
ObjectAlias xxx
lines
BeginKeyframe INT 12 ---->INT comes from sumKeys function
lines
EndObject
BeginAnotherObjects
...
你可以使用
tell
和seek
在文件中移动,所以要做你想做的事情,你可以使用这样的东西,我把它们拼凑在一起:这些评论几乎可以解释发生了什么。你知道吗
给定一个输入文件,比如
它将取代线路
与
相关问题 更多 >
编程相关推荐