0

我需要在文本文件中找到“翻译”的每个实例,并在找到文本后替换 4 行的值:

"(many lines)
                }
            }   
        translateX xtran        
            {   
            keys    
                {
                k  0  0.5678
                }
            }   
(many lines)"

值 0.5678 需要为 0。它将始终位于"translate"字符串下方 4 行。该文件最多有大约 10,000 行。示例文本文件名:01F.pz2.

我还想循环浏览文件夹并为每个带有pz2扩展名的文件(最多 40 个)重复该过程。

任何帮助,将不胜感激!

谢谢。

4

2 回答 2

1

我不太确定在您的文件中替换 0.5678 的逻辑,因此我使用了一个函数 - 将其更改为您需要的任何内容,或者更详细地解释您想要的内容。排队的最后一个数字?只有浮点数?

尝试:

import os    

dirname = "14432826"
lines_distance= 4

def replace_whatever(line):
    # Put your logic for replacing here
    return line.replace("0.5678", "0")

for filename in filter(lambda x:x.endswith(".pz2") and not x.startswith("m_"), os.listdir(dirname)):
    print filename
    with open(os.path.join(dirname, filename), "r") as f_in, open(os.path.join(dirname,"m_%s" % filename), "w") as f_out:
        replace_tasks = []
        for line in f_in:
            # search marker in line
            if line.strip().startswith("translate"):
                print "Found marker in", line,
                replace_tasks.append(lines_distance)                
            # replace if necessary
            if len(replace_tasks)>0 and replace_tasks[0] == 0:
                del replace_tasks[0]
                print "line to change is", line,
                line_to_write = replace_whatever(line)
            else:
                line_to_write = line
            # Write to output
            f_out.write(line_to_write)
            # decrease counters
            for i, task in enumerate(replace_tasks):
                replace_tasks[i] -= 1

代码中的注释应该有助于理解。主要概念是replace_tasks记录下一行要修改的时间的列表。

备注:您的代码示例表明您的文件中的数据是结构化的。阅读此结构并对其进行处理肯定会比在纯文本文件上进行搜索和替换方法更节省。

于 2013-01-21T06:53:22.547 回答
0

Thorsten,我将原始文件重命名为具有 .old 扩展名,并且以下代码有效:

import os
target_dir = "."


# cycle through files
for path, dirs, files in os.walk(target_dir):
    # file is the file counter
    for file in files:
        # get the filename and extension
        filename, ext = os.path.splitext(file)
        # see if the file is a pz2
        if ext.endswith('.old') :
            # rename the file to "old"
            oldfilename = filename + ".old"
            newfilename = filename + ".pz2"
            old_filepath = os.path.join(path, oldfilename)
            new_filepath = os.path.join(path, newfilename)
            # open the old file for reading
            oldpz2 = open (old_filepath,"r")
            # open the new file for writing
            newpz2 = open (new_filepath,"w")
            # reset changeline
            changeline = 0
            currentline = 0
            # cycle through old lines
            for line in oldpz2 :
                currentline = currentline + 1
                if line.strip().startswith("translate"):
                        changeline = currentline + 4
                if currentline == changeline :
                    print >>newpz2,"                k  0  0"
                else :
                    print >>newpz2,line
于 2013-01-21T17:11:28.467 回答