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 に答える