【发布时间】:2018-01-04 21:25:40
【问题描述】:
我使用的是 Synopsys 创建的 Coverity,主要由静态代码分析和动态代码分析工具组成。我目前正在针对我的代码运行它,因此它会生成一个 .xml 文件。
.xml 包含数字字符串,每个数字都有自己的含义。我想制作一个 Python33 脚本来打印直接映射到函数复杂性的第七个数字。我可以很好地将它打印到控制台,但由于我是 C 编码器,而且不仅是 Python 中的一部分,这就是我所拥有的:
*.xml 的行示例:
<fnmetric>
<file>FILE1.C</file>
<fnmet>function1;1;12;10;21;8;9;5;1441.75;0.0557199;;;318</fnmet>
</fnmetric>
<fnmetric>
<file>FILE2.C</file>
<fnmet>function2;0;0;1;11;8;3;1;184.638;0.0175846;;;352</fnmet>
</fnmetric>
所以第一个函数的第 7 个数字是 9,第二个是 3
到目前为止我有:
import fileinput
import glob
import re
import sys
#Read in all files in the directory ending in .xml
files = glob.glob('*.xml')
#search for the line beginning in <fnmet> and ending in </fnmet>
pattern = re.compile(r'<fnmet>\s+;([_A-Z]+)</fnmet>\s+$') #this line is wrong
realstdout = sys.stdout
#create .bak files of the .h files unchanged (backups)
for line in fileinput.input(files, inplace=True, backup='.bak'):
sys.stdout.write(line)
m = pattern.match(line)
#print the 7th #; as function + complextity through all .xml lines.
if m:
sys.stdout.write('\n <fnmet>\%s</fnmet>\n' % m.group(1)) #this line is wrong
realstdout.write('%s: %s\n'%(fileinput.filename(),m.group(1))) #this line is wrong
基本上我只是想在 function_name 之后打印第 6 个数字;
类似:
函数1:9 函数2:3
感谢任何帮助,我的 python 太可怕了。
【问题讨论】:
-
我们使用的是regex on x|html吗?
标签: python xml python-3.3 coverity