I'm writing a simple parser for ascii data, in which each row has to be interpreted as fields of 8-block of chars:
"""
|--1---||--2---||--3---||--4---||--5---||--6---||--7---||--8---||--9---|
GRID 119 18.27 562.33 528.87
"""
This row, should be interpreted as:
1: GRID + 4 blank spaces
2: 5 blank spaces + 119
3: 8 blank spaces
4: 3 blank spaces + 18.27
5: 2 blank spaces + 562.33
6: 2 blank spaces + 528.87
7: 8 blank spaces
8: 8 blank spaces
9: 8 blank spaces
This is what I've tried
EOL = LineEnd().suppress()
card_keyword = Keyword("GRID").leaveWhitespace().suppress()
number_card_fields = (number + ZeroOrMore(White()))
empty_card_fields = 8 * White()
card_fields = (number_card_fields | empty_card_fields)
card = (card_keyword + OneOrMore(card_fields)).setParseAction(self._card_to_dict)
def _card_to_dict(self, toks):
_FIELDS_MAPPING = {
0: "id", 1: "cp", 2: "x1", 3: "x2", 4: "x3", 5: "cd", 6: "ps", 7: "seid"
}
mapped_card = {self._FIELDS_MAPPING[idx]: token_field for idx, token_field in enumerate(toks)}
return mapped_card
test2 = """
GRID 119 18.27 562.33 528.87
"""
print(card.searchString(test2))
This return
[[{'id': 119, 'cp': ' ', 'x1': 18.27, 'x2': ' ', 'x3': 562.33, 'cd': ' ', 'ps': 528.87, 'seid': ' \n'}]]
I would like to obtain this, instead
[[{'id': 119, 'cp': ' ', 'x1': 18.27, 'x2': 562.33, 'x3': 528.87, 'cd': ' ', 'ps': ' ', 'seid': ' '}]]
I think the problem is here number_card_fields = (number + ZeroOrMore(White()))
. I don't know how to tell to pyparsing that this expresion must be exaclty 8 chars long.
Can someone help me?Thanks in advance for your valuable support