调用函数中的子函数来处理Python中的同一个文件?

2024-09-28 03:13:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我仍然在用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
...

Tags: inforiflinekeysfinditemblock
1条回答
网友
1楼 · 发布于 2024-09-28 03:13:41

你可以使用tellseek在文件中移动,所以要做你想做的事情,你可以使用这样的东西,我把它们拼凑在一起:

import re

# so, we're looking for the object 'HeyThere'
objectname = 'HeyThere'

with open('input.txt', 'r+') as f:
    line = f.readline()
    pos = f.tell()
    found = False
    while line:

        # we only want to alter the part with the 
        # right ObjectAlias, so we use the 'found' flag
        if 'ObjectAlias ' + objectname in line:
            found = True
        if 'EndObject' in line:
            found = False

        if found and 'BeginKeyframe' in line:

            # we found the Keyframe part, so we read all lines 
            # until EndKeyframe and count each line with 'default'
            sub_line = f.readline()
            frames = 0
            while not 'EndKeyframe' in sub_line:
                if 'default' in sub_line:
                    frames += 1
                sub_line = f.readline()

            # since we want to override the 'BeginKeyframe', we
            # have to move back in the file to before this line
            f.seek(pos)

            # now we read the rest of the file, but we skip the
            # old 'BeginKeyframe' line we want to replace
            f.readline()
            rest = f.read()

            # we jump back to the right position again
            f.seek(pos)

            # and we write our new 'BeginKeyframe' line
            f.write(re.sub('\d+', str(frames), line, count=1))

            # and write the rest of the file
            f.write(rest)
            f.truncate()
            # nothing to do here anymore, just quit the loop
            break

        # before reading a new line, we keep track
        # of our current position in the file
        pos = f.tell()
        line = f.readline()

这些评论几乎可以解释发生了什么。你知道吗

给定一个输入文件,比如

foo
bar
BeginObject
something
something
ObjectAlias NotMe
lines
more lines
BeginKeyframe 22 12   
foo
bar default
foo default
bar default
EndKeyframe
EndObject
foo
bar
BeginObject
something
something
ObjectAlias HeyThere
lines
more lines
BeginKeyframe 43243 12   
foo
bar default
foo default
bar default
foo default
bar default
foo default
bar
EndKeyframe
EndObject

它将取代线路

BeginKeyframe 43243 12   

BeginKeyframe 6 12   

相关问题 更多 >

    热门问题