I have file which I will be parsing repeatedly to check and edit the contents.
(我有要反复分析以检查和编辑内容的文件。)
Below is the snippet of the insert_code_file.txt.
(以下是insert_code_file.txt的代码段。)
//------------------------------------------
// Define Town Names
//-----------------------------------------
//------------------------------------------
// Define Color Names
//-----------------------------------------
//------------------------------------------
// Define City Names
//-----------------------------------------
Below is the snippet of the same file with some information added - insert_code_file1.txt
(以下是同一文件的片段,其中添加了一些信息-insert_code_file1.txt)
//------------------------------------------
// Define Town Names
//-----------------------------------------
//------------------------------------------
// Define Color Names
//------------------------------------------
`ifndef DISABLE_COLOR_NAMES
// blue
// green
`endif // DISABLE_COLOR_NAMES
//------------------------------------------
// Define City Names
//-----------------------------------------
Wanted to parse the file and add some text accordingly.
(想要解析文件并相应地添加一些文本。)
For eg:
(例如:)
In snippet 1, if the ifndef DISABLE_COLOR_NAMES isn't there, then the code need to append ifndef DISABLE_COLOR_NAME and add the respective text and close it with endif.
(在代码段1中,如果ifndef DISABLE_COLOR_NAMES不存在,则代码需要附加ifndef DISABLE_COLOR_NAME并添加相应的文本并使用endif将其关闭。)
If as shown in snippet 2, if the ifndef DISABLE_COLOR_NAMES already exists, then I need to skip adding that line and add the respective text alone and then again check if the endif is there and end accordingly.
(如果如代码段2所示,如果ifndef DISABLE_COLOR_NAMES已经存在,那么我需要跳过添加该行并单独添加相应的文本,然后再次检查endif是否存在并相应地结束。)
Tried below piece of code and kinda works for the first case [but know the code is something wrong] but for the 2nd case it messes up the file.
(在下面的代码段中进行了尝试,并且在第一种情况下可以工作[但知道代码有问题],但是在第二种情况下,它将文件弄乱了。)
# Import Variables
import Tkinter, Tkconstants, tkFileDialog
from Tkinter import *
import string
import ttk
import re
import gzip
import signal
import inspect
import textwrap
def add_edit_code(filename):
file_arr = []
file = open(filename,"r+")
fnd1 = 0
fnd2 = 0
fnd3 = 0
for lines in file:
file_arr.append(lines)
if (fnd1 == 0) and lines.startswith("// Define Color Names"):
fnd1 = 1
if (fnd1 == 1) and lines.startswith("//--"):
fnd1 = 0; fnd2 = 1;
if (fnd2 == 1) and not lines.startswith("`ifndef DISABLE_COLOR_NAMES"):
file_arr.append("`ifndef DISABLE_COLOR_NAMES
"); fnd2 = 0; fnd3 = 1
if (fnd3 == 1) and len(lines.strip()) != 0:
file_arr.append(" // red"); fnd3 = 1; fnd1 = 0;
else:
if (fnd3 == 1) and not lines.startswith("`endif // DISABLE_COLOR_NAMES") and len(lines.strip()) == 0:
file_arr.append("`endif // DISABLE_COLOR_NAMES
"); fnd3 = 0
else:
continue
file = open(filename, "w")
for lines in file_arr:
file.write(lines)
file.close()
add_edit_code("insert_code_file.txt")
add_edit_code("insert_code_file1.txt")
For the first file below is the output
(对于下面的第一个文件是输出)
For the 2nd file below is the output.
(下面的第二个文件是输出。)
Is there any better technique to parse, edit and append for the above scenario?
(对于上述情况,是否有更好的技术来解析,编辑和追加?)
ask by Vimo translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…