我需要在文本文件中找到“translate”的每个实例,并在找到文本后将值替换为4行:
"(many lines)
}
}
translateX xtran
{
keys
{
k 0 0.5678
}
}
(many lines)"
值0.5678必须为0.它总是比"translate"
字符串低4行
该文件最多有10,000行。
示例文本文件名:01F.pz2
。
我还想循环浏览文件夹,并为pz2
分机(最多40个)的每个文件重复此过程。
任何帮助将不胜感激!
感谢。
答案 0 :(得分: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
,它记录下一行修改的时间。
备注:您的代码示例表明文件中的数据是结构化的。阅读这个结构并在其上工作而不是纯文本文件上的搜索和替换方法肯定会更安心。
答案 1 :(得分: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