Python 如何从源代码文件的方法声明中删除方法参数并将其写回?

Python 如何从源代码文件的方法声明中删除方法参数并将其写回?,python,Python,我有一个包含预处理源代码的.txt文件。看起来是这样的: public static <V, E> E addEdgeWithVertices(Graph<V, E> g, V sourceVertex, V targetVertex) g.addVertex(sourceVertex); g.addVertex(targetVertex); return g.addEdge(sourceVertex, targetVertex); 实际输出(缺少剩余的行): publ

我有一个包含预处理源代码的.txt文件。看起来是这样的:

public static <V, E> E addEdgeWithVertices(Graph<V, E> g, V sourceVertex, V targetVertex)
g.addVertex(sourceVertex);
g.addVertex(targetVertex);
return g.addEdge(sourceVertex, targetVertex);
实际输出(缺少剩余的行):

public static E addedgeWithVertexts()
预期产出:

public static <V, E> E addEdgeWithVertices()
g.addVertex(sourceVertex);
g.addVertex(targetVertex);
return g.addEdge(sourceVertex, targetVertex);
public static E addedgeWithVertexts()
g、 addVertex(sourceVertex);
g、 addVertex(targetVertex);
返回g.addEdge(sourceVertex、targetVertex);

如果您知道它将始终位于第一行,您可以执行以下操作:

import re

with open('source_code.txt', 'r') as f:
        lines = f.readlines()
        match_parameters = re.search("(public|protected|private|static) .*\((.*)\)", lines[0])
        remove_parameters = match_parameters.group(0).replace(match_parameters[2], "")
        lines[0] = remove_parameters + "\n"
with open('source_code.txt', 'w') as f:
        f.writelines("".join(lines))

如果您知道它将始终位于第一行,您可以执行以下操作:

import re

with open('source_code.txt', 'r') as f:
        lines = f.readlines()
        match_parameters = re.search("(public|protected|private|static) .*\((.*)\)", lines[0])
        remove_parameters = match_parameters.group(0).replace(match_parameters[2], "")
        lines[0] = remove_parameters + "\n"
with open('source_code.txt', 'w') as f:
        f.writelines("".join(lines))

你应该搜索所有的行。当搜索返回
None
时,不要忘记添加原始行

clean_code = []
with open('source_code.txt', 'r') as f:
    lines = f.readlines()
    for line in lines:
        match_parameters = re.search("(public|protected|private|static) .*\((.*)\)", line)
        if match_parameters is not None:
            clean_code.append(match_parameters.group(0).replace(match_parameters[2], "") + "\n")
        else:
            clean_code.append(line)
with open('source_code.txt', 'w') as f:
    f.writelines(clean_code)

但是,如果事实上只能在第一行进行更换,请使用

您应该搜索所有行。当搜索返回
None
时,不要忘记添加原始行

clean_code = []
with open('source_code.txt', 'r') as f:
    lines = f.readlines()
    for line in lines:
        match_parameters = re.search("(public|protected|private|static) .*\((.*)\)", line)
        if match_parameters is not None:
            clean_code.append(match_parameters.group(0).replace(match_parameters[2], "") + "\n")
        else:
            clean_code.append(line)
with open('source_code.txt', 'w') as f:
    f.writelines(clean_code)

但是,如果事实上只能在第一行进行更换,请使用

你得到的实际输出是什么?@CalvinGodfrey更新了!您只在
行[0]
上调用
re.search
,因此
匹配\u参数
,反过来,
删除\u参数
只是第一行。@CalvinGodfrey那么我如何在保持其他行不变的同时替换第一行呢?如果您在所有行上调用
re.search
,它仍然应该只匹配第一行,其余部分保持不变。您得到的实际输出是什么?@CalvinGodfrey updated!您只在
行[0]
上调用
re.search
,因此
匹配\u参数
,反过来,
删除\u参数
只是第一行。@CalvinGodfrey那么我如何在保持其他行不变的同时替换第一行呢?如果您在所有行上调用
re.search
,它仍然应该只匹配第一行,并保持其余行不变。