如何使用python,pyparsing按顺序解析文件中的行/匹配足够的结果?

时间:2012-01-06 09:02:48

标签: python pyparsing

这是我的代码:

from pyparsing import *

survey ='''
BREAK_L,PN1000,LA55.16469813,LN18.15054629
PN1,LA54.16469813,LN17.15054629,EL22.222
BREAK_L,PN2000,LA55.16507249,LN18.15125566
PN6,LA54.16506873,LN17.15115798,EL33.333
PN7,LA54.16507249,LN17.15125566,EL44.444
BREAK_L,PN3000,LA55.16507249,LN18.15125566
PN10,LA54.16507522,LN17.15198405,EL55.555
PN11,LA54.16506566,LN17.15139220,EL44.44
PN12,LA54.16517275,LN17.15100652,EL11.111
'''

digits = "0123456789"
number = Word(nums+'.').setParseAction(lambda t: float(t[0]))
num = Word(digits)
text = Word(alphas)
pt_id = Suppress('PN') + Combine(Optional(text) + num + Optional(text) + Optional(num))
separator = Suppress(',')
latitude = Suppress('LA') + number
longitude = Suppress('LN') + number

gps_line = pt_id + separator + latitude + separator + longitude
break_line = (Suppress('BREAK_L,')
          + pt_id
          + separator
          + latitude
          + separator
          + longitude)

result1 = gps_line.scanString(survey)
result2 = break_line.scanString(survey)

for item in result1:
    print item



通过上面的例子,我想找到如何获得输出的解决方案:

gps_line +它的break_line,在伪代码中意味着什么:

for every gps_line in result1:
    print gps_line + precedent break_line


如果我的问题不明确或不符合描述,请随时更改。

编辑#2
我试图实现的是输出:

['1', 54.16469813, 17.15054629, 22.222, 'BP1000', 55.16469813, 18.15054629]
['6', 54.16506873, 17.15115798, 33.333, 'BP2000', 55.16507249, 18.15125566]
['7', 54.16507249, 17.15125566, 44.444, 'BP2000', 55.16507249, 18.15125566]
['10', 54.16507522, 17.15198405, 55.555, 'BP3000', 55.16507249, 18.15125566]
['11', 54.16506566, 17.1513922, 44.44, 'BP3000', 55.16507249, 18.15125566]
['12', 54.16517275, 17.15100652, 11.111, 'BP3000', 55.16507249, 18.15125566]

1 个答案:

答案 0 :(得分:1)

第二次尝试:

from decimal import Decimal
from operator import itemgetter
survey ='''
BREAK_L,PN1000,LA55.16469813,LN18.15054629
PN1,LA54.16469813,LN17.15054629,EL22.222
BREAK_L,PN2000,LA55.16507249,LN18.15125566
PN6,LA54.16506873,LN17.15115798,EL33.333
PN7,LA54.16507249,LN17.15125566,EL44.444
BREAK_L,PN3000,LA55.16507249,LN18.15125566
PN10,LA54.16507522,LN17.15198405,EL55.555
PN11,LA54.16506566,LN17.15139220,EL44.44
PN12,LA54.16517275,LN17.15100652,EL11.111
'''

def parse_line(line):
  brk = False
  kv = {}
  for part in line.split(','):
    if part == 'BREAK_L':
      brk = True
    else:
      k = part[:2]
      v = part[2:]
      kv[k] = v
  return (brk,kv)

def parse_survey(survey):
  ig1 = itemgetter('PN','LA','LN','EL')
  ig2 = itemgetter('PN','LA','LN')
  brk_data = None
  for line in survey.strip().splitlines():
    brk, data = parse_line(line)
    if brk:
      brk_data = data
      continue
    else:
      yield ig1(data) + ig2(brk_data)

for r in parse_survey(survey):
  print r

收率:

('1', '54.16469813', '17.15054629', '22.222', '1000', '55.16469813', '18.15054629')
('6', '54.16506873', '17.15115798', '33.333', '2000', '55.16507249', '18.15125566')
('7', '54.16507249', '17.15125566', '44.444', '2000', '55.16507249', '18.15125566')
('10', '54.16507522', '17.15198405', '55.555', '3000', '55.16507249', '18.15125566')
('11', '54.16506566', '17.15139220', '44.44', '3000', '55.16507249', '18.15125566')
('12', '54.16517275', '17.15100652', '11.111', '3000', '55.16507249', '18.15125566')

这与我之前的尝试没什么不同。我已经为你配对了这些数据。我假设您可以自己将1000更改为BP1000