1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
#! /usr/bin/python
# -*- coding utf-8 -*-
from __future__ import division
import re
class Reader(object):
_re = re.compile(r'^(?P<field>\d-\d:\d+\.\d+\.\d+\*\d+)\((?P<value>\S+?)(?:\*[VAW])?\)$')
def _convert_periode(value):
return int(value, 16) / 100
fields = {
'1-0:1.8.0*255': ('overall', float),
'1-0:31.7.0*255': ('l1_strom', float),
'1-0:32.7.0*255': ('l1_spannung', float),
'1-0:51.7.0*255': ('l2_strom', float),
'1-0:52.7.0*255': ('l2_spannung', float),
'1-0:71.7.0*255': ('l3_strom', float),
'1-0:72.7.0*255': ('l3_spannung', float),
'1-0:96.50.0*1': ('periode', _convert_periode),
}
def __init__(self, f):
self._file = f
def __iter__(self):
data = {}
for line in self._file:
line = line.strip()
if line == '!':
yield data
data = {}
continue
r = self._re.match(line)
if not r:
continue
field = self.fields.get(r.group('field'))
if field:
data[field[0]] = field[1](r.group('value'))
#uncomment to print unmapped values
#else:
# print r.groups()
data_file = open('testdata')
for data in Reader(data_file):
print data
|