两个文件,匹配日期以匹配数据,可以工作,但速度非常慢



我正在读取两个文件。一个有格式:

11 Mar 2020 12:34:38
Satellite
Time of Apogee (UTCG)      Time of Perigee (UTCG)
------------------------    ------------------------
1 Mar 2020 00:33:46.221     1 Mar 2020 01:24:13.803
1 Mar 2020 02:14:32.442     1 Mar 2020 03:05:00.106
1 Mar 2020 03:55:18.659     1 Mar 2020 04:45:46.406
1 Mar 2020 05:36:04.874     1 Mar 2020 06:26:32.703
1 Mar 2020 07:16:51.085     1 Mar 2020 08:07:18.996

另一种格式为:

11 Mar 2020 12:40:11
Satellite:  LLA Position

Time (UTCG)          Lat (deg)    Lon (deg)
------------------------    ---------    ---------
1 Mar 2020 00:00:00.000      -34.134      -97.662
1 Mar 2020 00:01:00.000      -30.417      -97.086
1 Mar 2020 00:02:00.000      -26.720      -96.577
1 Mar 2020 00:03:00.000      -23.048      -96.120
1 Mar 2020 00:04:00.000      -19.399      -95.707

我正在按时间进行匹配,并输出第一个文件时间和第二个文件lat/long。问题是它需要很长时间,因为这些都是大文件。我看到我从一开始就一遍又一遍地执行for循环,而不是从我停止的地方开始…

我该如何加快速度?

import datetime
with open("apFile.txt", "r+") as apFile:
with open("dates.txt", "r+") as file:
lines = file.readlines()
apLines = apFile.readlines()
margin = datetime.timedelta(seconds=30)
j = 0
for apLine in apLines:
j += 1
if j <= 6:
continue
apLines = apFile.readlines()
apLine_list = apLine.split()
ap_day = apLine_list[0]
ap_month = apLine_list[1]
ap_year = apLine_list[2]
ap_time = apLine_list[3]
apogee_time_datetime = datetime.datetime.strptime(ap_day + " " + ap_month + " " + ap_year + " " + ap_time, "%d %b %Y %H:%M:%S.%f")
pr_day = apLine_list[4]
pr_month = apLine_list[5]
pr_year = apLine_list[6]
pr_time = apLine_list[7]
perigee_time_datetime = datetime.datetime.strptime(pr_day + " " + pr_month + " " + pr_year + " " + pr_time, "%d %b %Y %H:%M:%S.%f")
i = 0
for line in lines:
i += 1
if i <= 6:
continue
line_list = line.split()
ll_day = line_list[0]
ll_month = line_list[1]
ll_year = line_list[2]
ll_time = line_list[3]
ll_lat = line_list[4]
ll_long = line_list[5]
ll_time_datetime = datetime.datetime.strptime(ll_day + " " + ll_month + " " + ll_year + " " + ll_time, "%d %b %Y %H:%M:%S.%f")
if apogee_time_datetime - margin <= ll_time_datetime <= apogee_time_datetime + margin:
print("Apogee: " + str(apogee_time_datetime) + " Lat: " + ll_lat + " Long: " + ll_long)

如果不想执行到循环结束,可以实现break语句。

如果你有一个排序的数据库,你可以实现一个搜索算法来减少执行时间。搜索算法

最新更新