用Python解析数据文件
我目前正在用python为x-plane做一个pet项目,但需要一些解析数据的帮助 这是一个数据样本用Python解析数据文件,python,Python,我目前正在用python为x-plane做一个pet项目,但需要一些解析数据的帮助 这是一个数据样本 1 196 1 0 EGKK London Gatwick 100 45.11 1 1 0.00 1 3 0 08R 51.14510986 -000.21231370 392.89 0.00 5 4 1 1 26L 51.15119014 -000.16819230 267.92 182.88 5 4 1 1 100 45.11 1 1 0
1 196 1 0 EGKK London Gatwick
100 45.11 1 1 0.00 1 3 0 08R 51.14510986 -000.21231370 392.89 0.00 5 4 1 1 26L 51.15119014 -000.16819230 267.92 182.88 5 4 1 1
100 45.11 1 1 0.00 0 3 1 08L 51.14691084 -000.21256968 320.96 0.00 4 7 0 1 26R 51.15180116 -000.17681432 416.05 0.00 4 7 0 1
21 51.14622685 -000.20070224 3 77.60 3.00 08R PAPI-4R
21 51.14935802 -000.17789218 2 257.60 3.00 26L PAPI-4L
110 2 0.00 77.70 Parking positions 101-103
111 51.15774074 -000.17453421
111 51.15612096 -000.17392833
111 51.15658181 -000.17088556
113 51.15811289 -000.17149989
111 51.15791243 -000.17250212
111 51.15761372 -000.17239479
111 51.15749171 -000.17330039
113 51.15779042 -000.17341443
110 1 0.00 167.70 Ramp
111 51.15286332 -000.16377606
111 51.15258969 -000.16312609
111 51.15454938 -000.16302507
113 51.15403028 -000.16372478
110 2 0.00 77.50 Twy J west
111 51.14804859 -000.21064157 51 102
111 51.14845633 -000.20754956 51 102
112 51.14845142 -000.20696022 51.14842303 -000.20680182 51 102
111 51.14822704 -000.20686650 51 102
111 51.14787041 -000.20703621
111 51.14809780 -000.20534823 51 102
112 51.14835596 -000.20601269 51.14854333 -000.20604889
111 51.14835596 -000.20601269
112 51.14835596 -000.20601269 51.14846668 -000.20603079 51 102
111 51.14853305 -000.20582527 51 102
111 51.14878419 -000.20507558 51 102
111 51.14915518 -000.20239537 51 102
111 51.15028333 -000.19414918 51 102
112 51.15015284 -000.19308931 51.14990429 -000.19299026 51 102
111 51.14977192 -000.19312473
111 51.14994515 -000.19188237 51 102
112 51.15024605 -000.19228698 51.15042624 -000.19235632 51 102
111 51.15068101 -000.19129646 51 102
111 51.15181209 -000.18271022
111 51.15207776 -000.18279411 51 102
111 51.15170518 -000.18556349
111 51.15134334 -000.18819268 51 102
112 51.15080761 -000.19200036 51.15080249 -000.19202760
111 51.15080761 -000.19200036
112 51.15080761 -000.19200036 51.15073926 -000.19263232 51 102
111 51.15139885 -000.19291562 51 102
111 51.15299423 -000.19345539 51 102
112 51.15345927 -000.19346592 51.15352079 -000.19342233
1 20 1 0 YPAD Adelaide Intl
100 46.02 1 2 0.25 1 3 1 05 -34.95852900 138.51720042 157.89 0.00 5 3 1 1 23 -34.94065900 138.54330673 0.00 61.87 5 3 1 0
100 46.02 1 0 0.25 0 2 1 12 -34.94117850 138.52191000 0.00 0.00 5 3 1 0 30 -34.94939000 138.53698400 0.00 0.00 5 3 1 0
102 H1 -34.93518312 138.53192488 231.46 29.87 29.87 2 0 0 0.00 1
102 H2 -34.94658683 138.52277413 356.62 41.15 39.93 2 0 0 0.00 1
21 -34.94333383 138.54033847 2 230.00 3.00 New PAPI-4L
21 -34.94261483 138.53961547 3 230.00 3.00 New PAPI-4R
21 -34.95335269 138.52397752 2 50.00 3.00 New PAPI-4L
21 -34.94767809 138.53483040 3 303.70 3.00 New PAPI-4R
21 -34.94847005 138.53417115 2 303.70 3.00 New PAPI-4L
21 -34.94320477 138.52444181 3 123.70 3.00 New PAPI-4R
21 -34.94241898 138.52511056 2 123.70 3.00 New PAPI-4L
110 1 1.00 123.70 New Taxiway 199
111 -34.94102671 138.52209460
111 -34.94093997 138.52194695
111 -34.94128895 138.52166396
113 -34.94136359 138.52179930
110 2 1.00 123.70 New Taxiway 213
111 -34.94720010 138.53242703
112 -34.94773614 138.53247063 -34.94793107 138.53226849
111 -34.94814549 138.53282734
111 -34.94759966 138.53339333
111 -34.94776762 138.53436555
112 -34.94716011 138.53434812 -34.94687064 138.53464022
111 -34.94705647 138.53349360
113 -34.94707434 138.53308815
110 4 1.00 0.00 New Taxiway 85
111 -34.93416388 138.53331538
112 -34.93417175 138.53324032 -34.93417896 138.53319052
111 -34.93421978 138.53219165
111 -34.93425647 138.53169827
112 -34.93427328 138.53165726 -34.93428528 138.53164555
111 -34.93435973 138.53166019
111 -34.93474635 138.53169242
111 -34.93546623 138.53082742
112 -34.93547218 138.53049092 -34.93548608 138.53048365
112 -34.93559920 138.53043281 -34.93563294 138.53041587
111 -34.93572637 138.53040795
111 -34.93566141 138.53051888
以1开头的每个行标题表示机场,所有后续行标题表示机场的不同方面,例如:
111 Plain Node
112 Node with Bezier Control Point
113 Node Close Loop to close boundary
114 Node Close Loop with Bezier Control Loop
115 Node to Terminate Line
116 Node with Bezier Control Point
那么,我如何解析这个数据文件,并仅读取/使用与特定数据块相关的信息,即:在1之间,然后进一步深入使用机场数据等。我建议您做的第一件事是编写一些代码,允许您检查是否正在处理新机场,但这不会占用该行。因此,在处理机场特定代码时,您知道下一个代码何时开始 更困难的是,制作一个可以让你读一行并把它推回的东西 一旦您能够真正解析这些行并找到新机场,您就可以使用它来创建一个机场对象,该对象知道如何处理不同的非1代码,并根据实际代码填写其信息
#! /usr/bin/env python
# coding: utf-8
from __future__ import print_function
class ParseAirportInfo(object):
def __init__(self, filename):
self._fp = open(filename)
self._line = None
def check(self):
if self._line is None:
self._line = self._fp.readline()
if self._line == '':
return -1
split_line = self._line.split(None, 1)
if split_line and split_line[0] == '1':
return 1
return 0
def read(self):
if self._line:
retval = self._line
self._line = None
return retval
return self._fp.readline()
class Airport(object):
def __init__(self, line):
# split the line starting with 1 and process the info
pass
def process_line(self, line):
# split the line and process depending on the first value
if line == '\n':
return
code, rest = line.split(None, 1)
code = int(code)
if code == 111:
pass
elif code == 110:
pass
airports = []
def main():
pai = ParseAirportInfo('airport.txt')
airport = None
while not pai.check() == -1:
if pai.check() == 1:
airport = Airport(pai.read())
while pai.check() == 0:
l = pai.read()
airport.process_line(l)
main()
有几种方法可以解析文本文件。在您的情况下,最简单的方法可能是使用“csv”模块。首先尝试一些代码,如果您有任何问题,可以问一个更具体的问题。@Whitetimer您不会,因为没有必要。OP在文本和标记中明确提到了一个Python程序。(然而,两者都是OOPL,这在结构上会使它们相同。尝试一下,当你陷入困境时,在SO上发布一个新问题,如果这有助于提供上下文,请参考此问题)。