Bạn có thể bắt đầu với một cái gì đó như thế này:
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)
Đầu ra của kịch bản là:
[52.125133215643, 21.031048525561, 116.898812]
Chỉnh sửa: Bạn cũng có thể muốn xem xét lepl, mà là một thư viện tương tự đó là khá tài liệu đẹp. Kịch bản tương đương với đoạn mã trên là:
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)
Cảm ơn rất nhiều sự giúp đỡ của bạn! – daikini
Chắc chắn, tôi sẽ kiểm tra mô-đun này – daikini
Nếu tôi muốn có '1' trong danh sách thì sao? Điều này sẽ trả về '1.0' vì nó sử dụng kiểu cast để float. là có cách nào để đúc chỉ này vào int? –