は、あなたがこのような何かを始めることができます:
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)
ありがとうございます。 – daikini
確かに、このモジュールをチェックします – daikini
リストに '1'をもたせたいのですが? floatに型キャストを使用しているので、これは '1.0'を返します。これをちょうどintにキャストする方法はありますか? –