这是我想要实现的目标:我想要替换
void some_function(int, int *, float);
带
void some_function(int a, int *b, float c);
到目前为止,我试图用chr(97 + i)替换“,”,因为我循环遍历字符串。
text = len("void some_function(int, int *, float);")
i = 0
for j in range(0, length)
if text[j] == ",":
rep = " " + chr(97+i) + " .,"
text = text.replace("," rep)
i = i + 1
j = 0 # resetting the index, but this time I want to find the next occurrence of "," which I am not sure
但这不是在做这项工作。如果有更好的方法,请告诉我。
答案 0 :(得分:1)
import re
i = 96
def replace(matches):
global i
i += 1
return " " + chr(i) + matches.group(0)
re.sub(',|\)', replace, "void some_function(int, int *, float);")
答案 1 :(得分:1)
text = "void some_function(int, int *, float);".replace(',', '%s,') % (' a', 'b')
答案 2 :(得分:0)
最初的想法不起作用,因为最后一个参数后面没有','而是'''。
import re
import string
def fix(text):
n = len(re.findall('[,(]', text))
return re.sub('([,)])', ' %s\\1', text) % tuple(string.letters[:n])