我正在编写一个用于替换行的python脚本 基本上,我想要一些行,比如说“ObjectAlias Here”部分| bewteen“BeginKeyframe”和“EndKeyframe”|每个“默认”下的前6行不以“ - ”开头
我的文件的主要部分如下:
# ...
# BeginObject
# lines
# ObjectAlias XXX
# lines
# BeginKeyframe
# -1.000 24 default
# 12 lines # not here since "default" line starts with "-"
# 0.000 24 default
# 12 lines # first 6 lines need to be modified
# 1.000 24 default
# 12 lines # first 6 lines need to be modified
# ...
# EndKeyframe
# lines
# EndObject
# {
# next object YYY section
# }
# ...
这是我现在的代码,它只在第一个“默认”下成功而没有以“ - ”开头,我知道这是因为我将标签设置为False,但我必须确保只在前面修改前6行“默认”。 我目前的代码怎么办?
def update(fileobj, objlist):
fileobj.seek(0)
tmpscene = ""
tag = False
count = 0
obj = None
for line in fileobj:
if tag and count < 6:
offset = t_objdict[obj].split(",")[count]
mod = " " + convert(line, offset) + line.split(None, 1)[-1]
tmpscene += mod
count += 1
if count > 5:
tag = False # Here is my problem!
continue
tmpscene += line
if "ObjectAlias" in line:
for obj in objlist:
if "ObjectAlias " + obj + "\n" in line:
for line in fileobj:
tmpscene += line
if line.strip().endswith("default") and not line.strip().startswith("-"):
tag = True
count = 0
break
break
return tmpscene
**编辑:**重写我的代码,仍然无法正常工作。