繁体   English   中英

解析和编辑/附加文件在 Python 中没有按预期发生

[英]Parsing and Editing/Appending a File isn't happening as expected in Python

我有我将反复解析以检查和编辑内容的文件。

下面是 insert_code_file.txt 的片段。

//------------------------------------------
// Define Town Names
//-----------------------------------------

//------------------------------------------
// Define Color Names
//-----------------------------------------

//------------------------------------------
// Define City Names
//-----------------------------------------

下面是添加了一些信息的同一个文件的片段 - insert_code_file1.txt

//------------------------------------------
// Define Town Names
//-----------------------------------------

//------------------------------------------
// Define Color Names 
//------------------------------------------
`ifndef DISABLE_COLOR_NAMES
 // blue
 // green 
`endif // DISABLE_COLOR_NAMES

//------------------------------------------
// Define City Names
//-----------------------------------------

想要解析文件并相应地添加一些文本。

例如:

  • 在片段 1 中,如果 ifndef DISABLE_COLOR_NAMES 不存在,则代码需要附加 ifndef DISABLE_COLOR_NAME 并添加相应的文本并使用 endif 将其关闭。

  • 如果如代码段 2 所示,如果 ifndef DISABLE_COLOR_NAMES 已经存在,那么我需要跳过添加该行并单独添加相应的文本,然后再次检查 endif 是否存在并相应地结束。

在下面的一段代码中尝试过,对于第一种情况有点工作[但知道代码有问题]但对于第二种情况,它弄乱了文件。

# 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\n"); 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\n\n"); 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")    

下面的第一个文件是输出

在此处输入图片说明

下面的第二个文件是输出。

在此处输入图片说明

有没有更好的技术来解析、编辑和附加上述场景?

更新的代码:在一些帮助下能够以不同的方法解决问题。 但是面临一个不同的问题,比如 ifndef 是否存在@多个地方,代码会在第一次出现时插入内容,而不是在我想要的地方。

在字符串过程中是否有任何方法可以在特定位置后检查子字符串?

# Global Import Variables
import Tkinter, Tkconstants, tkFileDialog
from   Tkinter import *
import subprocess
import shlex
import os 
import time
import string
import threading
import sys, argparse
import ttk
import re
import logging
import warnings
import os.path
import gzip
import signal
import inspect
import textwrap


def add_edit_code(filename):
    file_arr = []
    final_file_arr = []
    file = open(filename,"r+") 
    content = '' 

    for lines in file:
        content = content+lines+'|' 

    pos = content.find('`ifndef DISABLE_COLOR_NAMES')
    if pos == -1:
        pos = content.find('// Define Color Names\n')
        apos = pos + len('// Define Color Names\n')+len('//------------------------------------------\n')
        content = content[:apos]+'\n|`ifndef DISABLE_COLOR_NAMES\n| // red;\n|`endif // DISABLE_COLOR_NAMES'+content[apos:]
    else:
        apos = pos + len('ifndef DISABLE_COLOR_NAMES\n')
        content = content[:apos]+'\n // red;|'+content[apos:]

    file = open(filename, "w")
    final_file_arr = content.split('|')
    for lines in final_file_arr:
        file.write(lines)
    file.close()

add_edit_code("insert_code_file.txt")    
add_edit_code("insert_code_file1.txt")

您一次处理一条记录,而实际上一次必须处理两条记录。 当前和下一个记录。 对于file1.txt,代码

file_arr.append("`ifndef DISABLE_COLOR_NAMES\n"); 

在阅读下一行之前被执行。 所以`ifndef DISABLE_COLOR_and
// 红色被附加,然后 'ifndef DISABLE_COLOR 被附加到 file1.txt 中的 file_arr

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM