这是我第一次尝试使用pyparsing,我想问一下如何过滤这个样本行:
survey = '''GPS,PN1,LA52.125133215643,LN21.031048525561,EL116.898812'''输出如下: 1,52.125133215643,21.031048525561,116.898812
一般来说,我在理解pyparsing逻辑方面有问题,所以对这个例子的任何帮助都将不胜感激。谢谢
发布于 2011-12-15 00:24:37
您可以从如下内容开始:
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)https://stackoverflow.com/questions/8507694
复制相似问题