- 论坛徽章:
- 1
|
本帖最后由 shihyu 于 2017-07-14 20:00 编辑
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- import re
- import sys
- def get_nodes(inputline):
- p = re.compile("g\=\(([-+]?\d+\.\d+)\,\s([-+]?\d+\.\d+),\s([-+]?\d+\.\d+).*a\=\(([-+]?\d+\.\d+),\s([-+]?\d+\.\d+),\s([-+]?\d+\.\d+)")
- lines = inputline.read()
- lines = lines.strip().split('\n')
- lens = len(lines)
- print(lens)
- s0 = 0
- s1 = 0
- s2 = 0
- s3 = 0
- s4 = 0
- s5 = 0
- for i in range(0, lens, 1):
- n1 = p.findall(lines[i])[0]
- s0 = s0 + float(n1[0])
- s1 = s1 + float(n1[1])
- s2 = s2 + float(n1[2])
- s3 = s3 + float(n1[3])
- s4 = s4 + float(n1[4])
- s5 = s5 + float(n1[5])
- print(s0 / lens)
- print(s1 / lens)
- print(s2 / lens)
- print(s3 / lens)
- print(s4 / lens)
- print(s5 / lens)
- if __name__ == "__main__":
- fin = open(sys.argv[1])
- get_nodes(fin)
复制代码
g=(0.00000, -0.00195, 0.00000), a=(9.13281, -0.03906, 0.56836)
g=(0.00000, -0.00195, 0.00000), a=(9.12305, -0.05859, 0.57813)
g=(0.00000, -0.00195, 0.00000), a=(9.06445, -0.04883, 0.57813)
g=(0.00000, -0.00195, 0.00000), a=(9.04492, -0.02930, 0.57813)
g=(0.00000, -0.00195, 0.00000), a=(9.07422, -0.05859, 0.59766)
g=(0.00000, -0.00195, 0.00000), a=(9.02539, -0.04883, 0.58789)
g=(0.00000, -0.00195, 0.00000), a=(9.05469, -0.06836, 0.52930)
g=(0.00000, -0.00195, 0.00000), a=(9.11328, -0.03906, 0.63672)
g=(0.00000, -0.00195, 0.00000), a=(9.08398, -0.06836, 0.55859)
g=(0.00000, -0.00195, 0.00000), a=(9.06445, -0.06836, 0.57813)
g=(0.00000, -0.00195, 0.00000), a=(9.12305, -0.06836, 0.62695)
g=(0.00000, -0.00195, 0.00000), a=(9.10352, -0.05859, 0.57813)
g=(0.00000, -0.00195, 0.00000), a=(9.08398, -0.07813, 0.57813)
我目前是用 python regular expression 计算出每列数值总和的平均~ 不知道用 sed or awk 一行代码有办法作到同样事情嘛?
谢谢
|
|