Python 将执行日志合并到报告中
为了获得有关每次执行的有趣信息,我在C源代码中插入了一些频闪。 目前,每次执行时,我都会将信息打印到文件中。我有这样的清单:Python 将执行日志合并到报告中,python,c,matlab,logging,report,Python,C,Matlab,Logging,Report,为了获得有关每次执行的有趣信息,我在C源代码中插入了一些频闪。 目前,每次执行时,我都会将信息打印到文件中。我有这样的清单: 物业1:价值1 物业2:价值2 {这些值与2个不同的执行相关} 物业1:价值3 物业2:价值4 我想要的是: 属性1:值1+值3 物业2:价值2+价值4 我想以一种有效的方式生成一份报告。我更喜欢使用MATLAB或Python。 用户roippi给了我一个很好的Python答案。我也对在MATLAB中做同样的事情感兴趣 有指针吗 这是我的实际输入: S[10]1
- 物业1:价值1
- 物业2:价值2
- {这些值与2个不同的执行相关}
- 物业1:价值3
- 物业2:价值4
- 属性1:值1+值3
- 物业2:价值2+价值4
- S[10]1
- S[0]1
- S[1]1
- S[2]0
- S[3]1
- S[4]1
- S[5]4
- S[6]4
- S[7]0
- S[8]0
- S[9]1
- S[10]1
- S[0]1
- S[1]1
- S[2]0
- S[3]1
- S[4]1
- S[5]4
- S[6]4
- S[7]0
- S[8]0
- S[9]1
infile = file("input.txt", 'r')
outfile = file("output.txt", 'w')
from collections import defaultdict
props = defaultdict(int)
for line in infile:
p, v = line.split()
props[p] += int(v)
for p, v in sorted(props.items()):
outfile.write("%s: %d\n" % (p,v))
填充:
prop1 thing
prop2 stuff
prop3 junk
prop1 something
prop2 whatever
python:
from collections import defaultdict
d = defaultdict(list)
with open('infile') as f:
for line in f:
k,v = line.strip().split()
d[k].append(v)
然后格式化输出:
for k,v in sorted(d.items()):
print('{}: {}'.format(k,'+'.join(v)))
prop1: thing+something
prop2: stuff+whatever
prop3: junk
将
print
替换为您想要执行的任何实际文件写入操作。那么您希望将日志文件后处理到另一个文件中?在python中非常简单:
infile = file("input.txt", 'r')
outfile = file("output.txt", 'w')
from collections import defaultdict
props = defaultdict(int)
for line in infile:
p, v = line.split()
props[p] += int(v)
for p, v in sorted(props.items()):
outfile.write("%s: %d\n" % (p,v))
填充:
prop1 thing
prop2 stuff
prop3 junk
prop1 something
prop2 whatever
python:
from collections import defaultdict
d = defaultdict(list)
with open('infile') as f:
for line in f:
k,v = line.strip().split()
d[k].append(v)
然后格式化输出:
for k,v in sorted(d.items()):
print('{}: {}'.format(k,'+'.join(v)))
prop1: thing+something
prop2: stuff+whatever
prop3: junk
用您想要执行的任何实际文件写入操作替换
print
。如果该值是一个整数,并且您要为一个属性添加所有值,python中还有另一种方法:
infile = file("input.txt", 'r')
outfile = file("output.txt", 'w')
from collections import defaultdict
props = defaultdict(int)
for line in infile:
p, v = line.split()
props[p] += int(v)
for p, v in sorted(props.items()):
outfile.write("%s: %d\n" % (p,v))
输出将被排序,但这不是自然排序,您必须为此添加更多的python代码。如果该值是一个整数,并且您要添加属性的所有值,python中还有另一种方法:
infile = file("input.txt", 'r')
outfile = file("output.txt", 'w')
from collections import defaultdict
props = defaultdict(int)
for line in infile:
p, v = line.split()
props[p] += int(v)
for p, v in sorted(props.items()):
outfile.write("%s: %d\n" % (p,v))
输出将被排序,但这不是自然排序,您必须为此添加更多的python代码。到目前为止,有任何指向您的工作的指针吗?我不确定我是否理解这个问题。你能给出一个输入和输出的具体(格式化)例子吗?当然。在MATLAB中,我使用textscan从文件中读取格式化数据。所以我有一个细胞阵列。但我仍然想知道,使用多个文件连接是否更优雅。是的。输入是具体的,而且非常简单:对于C应用程序的每次执行,我都会输入信息。目前,该信息包含一个计数器ID(我把它想象成一个字符串)和一个整数值,例如0,1,2。它们之间用空格隔开。我们有:我们有:我们有:我们有:U U S[10]10[10]10[10]1 8 8 8 8[1]1 8 8[1]1 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8[1]1 8 8 8[2]0 0 0 0 8 8 8[2]0 0 8 8 8[2]0 8 8 8 8 8 8 8[1 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8[8 8 8 8 8 8 8 8 8 8 8[8 8 8 8 8 8 8 8 8 8 8 8 8 8 8[8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8[6]4*\u S_7]0*\u S_8]0*\u S_9]1到目前为止,你的努力有什么迹象吗?我不确定我是否理解这个问题。你能给出一个输入和输出的具体(格式化)例子吗?当然。在MATLAB中,我使用textscan从文件中读取格式化数据。所以我有一个细胞阵列。但我仍然想知道,使用多个文件连接是否更优雅。是的。输入是具体的,而且非常简单:对于C应用程序的每次执行,我都会输入信息。目前,该信息包含一个计数器ID(我把它想象成一个字符串)和一个整数值,例如0,1,2。它们之间用空格隔开。我们有:我们有:我们有:我们有:U U S[10]10[10]10[10]1 8 8 8 8[1]1 8 8[1]1 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8[1]1 8 8 8[2]0 0 0 0 8 8 8[2]0 0 8 8 8[2]0 8 8 8 8 8 8 8[1 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8[8 8 8 8 8 8 8 8 8 8 8[8 8 8 8 8 8 8 8 8 8 8 8 8 8 8[8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8[6]4*\u S_7]0*\u S_8]0*\u S_9]1我喜欢你的答案:干净高效!如果我也想在MATLAB中做同样的事情,我是不是问得太多了?我很喜欢你的答案:干净高效!如果我也希望在MATLAB中做同样的事情,我会问太多吗?你想过defaultdict(int)吗?我想如果代码接近C编程,你会喜欢它的。为了简洁,我现在已经做了您想要的更改。您想过defaultdict(int)吗?我想如果代码接近C编程,您会喜欢它的。对于紧凑性,我现在已经根据您的需要进行了更改。