Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
476 views
in Technique[技术] by (71.8m points)

python - 解析和编辑/附加文件未在Python中按预期进行(Parsing and Editing/Appending a File isn't happening as expected in Python)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are processing one record at a time, when actually you have to process two records at a time.

(您一次要处理一条记录,而实际上您必须一次处理两条记录。)

The current and the next record.

(当前和下一个记录。)

For file1.txt, the code

(对于file1.txt,代码)

file_arr.append("`ifndef DISABLE_COLOR_NAMES
"); 

gets executed before reading the next line.

(在读取下一行之前被执行。)

So `ifndef DISABLE_COLOR_and

(所以`ifndef DISABLE_COLOR_and)
// red gets appended and then 'ifndef DISABLE_COLOR gets appended to file_arr from file1.txt

(//添加红色,然后将'ifndef DISABLE_COLOR从file1.txt添加到file_arr)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...