这是我第一次尝试使用pyparsing,我想问一下如何过滤这个样本行:
survey = '''GPS,PN1,LA52.125133215643,LN21.031048525561,EL116.898812'''
Run Code Online (Sandbox Code Playgroud)
获得如下输出:1,52.125133215643,21.031048525561,116.898812
一般来说,我有理解pyparsing逻辑的问题,所以对这个例子的任何帮助将不胜感激.谢谢
jco*_*ado 23
你可以从这样的事情开始:
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)
Run Code Online (Sandbox Code Playgroud)
脚本的输出是:
[52.125133215643, 21.031048525561, 116.898812]
Run Code Online (Sandbox Code Playgroud)
编辑:您可能还想考虑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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6430 次 |
| 最近记录: |