这是我第一次尝试使用pyparsing,我想问一下 如何过滤此样本行:
survey = '''GPS,PN1,LA52.125133215643,LN21.031048525561,EL116.898812'''
得到如下输出:1,52.125133215643,21.031048525561,116.898812
总的来说,我对理解pyparsing逻辑有问题,所以对此有任何帮助 例子将不胜感激。感谢
答案 0 :(得分:21)
你可以从这样的事情开始:
from pyparsing import *
survey = '''GPS,PN1,LA52.125133215643,LN21.031048525561,EL116.898812'''
number = Word(nums+'.').setParseAction(lambda t: float(t[0]))
separator = Suppress(',')
latitude = Suppress('LA') + number
longitude = Suppress('LN') + number
elevation = Suppress('EL') + number
line = (Suppress('GPS,PN1,')
+ latitude
+ separator
+ longitude
+ separator
+ elevation)
print line.parseString(survey)
脚本的输出是:
[52.125133215643, 21.031048525561, 116.898812]
编辑:您可能还想考虑lepl,这是一个非常好的文档库。与上面相同的脚本是:
from lepl import *
survey = '''GPS,PN1,LA52.125133215643,LN21.031048525561,EL116.898812'''
number = Real() >> float
with Separator(~Literal(',')):
latitude = ~Literal('LA') + number
longitude = ~Literal('LN') + number
elevation = ~Literal('EL') + number
line = (~Literal('GPS')
& ~Literal('PN1')
& latitude
& longitude
& elevation)
print line.parse(survey)