1

我对 Python 真的很陌生,我正在尝试构建一个脚本来更改#define 变量的值。我的代码似乎可以工作,但是它破坏了输出文件的 C 缩进。那么如何解决空白问题呢?任何比我更聪明的实施建议都将受到高度赞赏!

KEYWORDS=["PERIOD","PWM_RATE","DUTY_OFFSET","MAX_DUTY"]
VALS=[3,3,3,3]    
import re
f1 = open('../src/in.c','r')
f2 = open('../src/out.xc','w')
for line in f1:
    s=line.split()
    if len(s)> 1 and s[1] in KEYWORDS:
        s[2] = VALS[1]
    f2.write(' '.join(s)+'\n')
f1.close()
f2.close()
4

4 回答 4

3

使用regex它会保持行中单词之间的原始间距:

使用with语句处理文件,因为它会自动为您关闭文件。

with open('../src/in.c','r') as f1, open('../src/out.xc','w') as f2:
    for line in f1:
        if line.startswith("#define"):
            s=line.split()
            if s[1] in KEYWORDS:
                val = str(VALS[1])
                line = re.sub(r'({0}\s+)[a-zA-Z0-9"]+'.format(s[1]),r"\g<1>{0}".format(val),line)
        f2.write(line)

输入:

#define PERIOD  100
#define PWM_RATE  5
#define DUTY_OFFSET  6
#define MAX_DUTY             7
#define PERIOD 2     5000000

#include<stdio.h>  
int main()  
{  
    int i,n,factor;  
    printf("Enter the last number of the sequence:");  
    scanf("%d",&j);
} 

输出:

#define PERIOD  3
#define PWM_RATE  3
#define DUTY_OFFSET  3
#define MAX_DUTY             3
#define PERIOD 3     5000000

#include<stdio.h>
int main()
{
int i,n,factor;
printf("Enter the last number of the sequence:");
scanf("%d",&j);
}
于 2013-05-16T19:05:13.077 回答
1

您可以只在字符串中使用替换,而不是标记整个源文件,如下所示:

for line in f1:
    for i in range(len(KEYWORDS)):
        line = line.replace("#define " + KEYWORDS[i], "#define " + KEYWORDS[i] + " " + str(VALS[i]))
    f2.write(line)

实际上,这不适用于已经具有值的变量,它不会替换它们的旧值,只会附加到它们。

所以OP建议的解决方案不是替换行中的字符串,而是像这样简单地重写整行:

for line in f1:
    for i in range(len(KEYWORDS)):
        if line.startswith("#define") and KEYWORDS[i] in line:
            line = "#define " + KEYWORDS[i] + " " + str(VALS[i])+"\n"
    f2.write(line)

另一种解决方案是使用正则表达式(re.sub()而不是line.replace()

于 2013-05-16T19:08:53.483 回答
1

就像是:

replace = {
    'PERIOD': '3'
}

with open('/home/jon/blah.txt') as fin, open('/home/jon/blah2.txt', 'w') as fout:
    lines = (line.strip() for line in fin)
    for line in lines:
        parts = line.split(None, 2)
        if parts[0].lower() == '#define':
            parts[2] = replace.get(parts[1], parts[2])
            fout.writeline(' '.join(parts) + '\n')
        else:
            fout.writeline(line + '\n')

所以这只会做以#define- 开头的行,直到下一个空格的任何内容都是要查找的名称,如果在替换中找到,将替换该值,否则,保持原样。

于 2013-05-16T19:09:38.027 回答
0

我认为问题在于.split()会忽略前导空格。

我会使用正则表达式来捕获前导空格并将您的输出附加到它捕获的内容中:

import re
line ='\t#define x'
split = line.split()
begin = re.match('^\s', line)
print(line[begin.start():begin.end()]+' '.join(split))
于 2013-05-16T19:06:29.233 回答