0

您好,我有一个文件 100 个文件的列表。

每个文件包含

a = a & "blahblhablhablha"
a = a & "blahblhablhablha"
a = a & "blahblhablhablha"
a = a & "blahblhablhablha"

我希望所有文件都应该像这样替换

public file1
a1 = a1 & "blahblhablhablha"
a1 = a1 & "blahblhablhablha"
a1 = a1 & "blahblhablhablha"
a1 = a1 & "blahblhablhablha"
a1x = a1
end file

在文件二中应该是

public file2
a2 = a2 & "blahblhablhablha"
a2 = a2 & "blahblhablhablha"
a2 = a2 & "blahblhablhablha"
a2x = a2
end file

等等……直到最后 100 个文件

假设我们的最后一个文件看起来像

public file100
a100 = a100 & "blahblhablhablha"
a100 = a100 & "blahblhablhablha"
a100 = a100 & "blahblhablhablha"
a100x = a100
end file
4

1 回答 1

1

您根本无法在 Notepad++ 中做到这一点!

原因是您在使用正则表达式时无权访问您编辑的文件的名称,也无权访问已编辑文件的计数器(这就是您在您的情况下编辑文件的方式)。理论上,可以通过手动处理每个文件并每次硬编码文件名来做到这一点,但正如 jgritty 建议的那样,最好的做法是使用更合适的工具,如 sed 或 Python、Perl 等。

因此,这是您的问题的 Python 解决方案:

from os import listdir
from os.path import isfile,join

path="yourfolder" #change it to your path of files

for filename in listdir(path): #read all entries from path
    if isfile(join(path,filename)): #keep only the files
        with open(join(getcwd(),path,filename),"r+") as file: #open each file
            num=filename #I assume that the files are named 1,...,100. If that's not the case then change this to a counter
            content=file.readlines() #pull the file contents
            content.insert(0,"public file%s\n" % num) #insert the 1st line
            content.append("a%sx = a%s\nend file\n" % (num,num)) #append the last lines
            file.seek(0) #reset file's current position in order to overwrite it
            for line in content:
                line=line.replace("a = a","a%s = a%s" % (num,num)) #edit the lines
                file.write(line) #write the output
于 2013-11-10T07:49:52.397 回答