所以基本上,我正在尝试浏览.txt文件,查找条目,然后编辑下面的下一个条目。
文字样本:
POINT
8
InvUndamaged
62
253
10
0.279006
20
基本上,我需要搜索“62”,然后用“256”替换它下面的数字。在此示例中,这意味着“253”将变为“256”。
txt_list = []
with open("X:\- Photogrammetry\Python to Correct DXF Colors\sample.dxf", "r") as x:
for line in x:
line = line.rstrip()
txt_list.append(line)
for i in txt_list:
if i == " 62":
x = txt_list.index(i) + 1
while x < len(txt_list):
txt_list[x] = "256"
x += 1
print(txt_list)
答案 0 :(得分:0)
我通过创建一个空列表,打开文件然后将每行(没有换行符)附加到列表来完成此操作。然后遍历列表,如果元素是“62”,则替换列表+1中的“62”索引并将其更改为“256”
txt_list = []
with open("filename.txt", "r") as x:
for line in x:
line = line.rstrip()
txt_list.append(line)
i = 0
while i < len(txt_list):
if txt_list[i] == "62":
txt_list[i+1] = "256"
i += 1