我有一个像这样的文本文件:
WP 000 Name Mumbai ANCHORAGE
Lat 36°10.140000N Lon 5°23.860000W
RL (Rumb Line)
XTE= 0.30nm
TurnRadius= 0.50nm
WP 001 Name Mumbai PILOT OFF
Lat 36°08.200000N Lon 5°23.770000W
RL (Rumb Line)
XTE= 0.20nm
TurnRadius= 0.00nm
我想以此创建一个数据框,列在下面,但根据文件而有所不同。
WP Name PILOT LAT Lon RL XTE TurnRadius
有人可以建议解决此问题的适当方法吗?
任何帮助都会得到感谢。
答案 0 :(得分:0)
尝试一下。我已经阅读了文件并解析了参数。您可以对代码进行进一步的增强。
import re
import pandas as pd
from collections import ChainMap
s = []
with open('readerfile.txt') as rf:
s = [i.strip('\n') for i in rf.readlines()]
sections = []
while(len(s)):
try:
if s.index('') != 0:
sections.append(s[:s.index('')])
if len(s)>1:
s = s[s.index('')+1:]
else:
s = []
except Exception as e:
print(e)
sections.append(s)
s = []
def get_groups(l):
d = []
while(len(l)):
d.append(l[:2])
l=l[2:]
return dict(d)
pd.DataFrame([ChainMap(*[get_groups(j.split() if len(j.split())%2==0 else j.split()+[None]) for j in i]) for i in sections])
这将呈现以下DataFrame:
ANCHORAGE Lat Lon Name PILOT RL TurnRadius= WP XTE=
0 NaN 36°10.140000N 5°23.860000W Mumbai NaN None 0.50nm 000 0.30nm
1 NaN 36°08.200000N 5°23.770000W Mumbai OFF None 0.00nm 001 0.20nm