运用PyQt5生成可执行小程序:匹配起始关键词到截止关键词区间的pos文件的内容,UTC时间转GPS时间,并清除过程中产生的复制文件。
pos文件如下:
% (x/y/z-ecef=WGS84,Q=1:fix,2:float,3:sbas,4:dgps,5:single,6:ppp,ns=# of satellites
% GPST x-ecef(m) y-ecef(m) z-ecef(m) Q ns sdx(m) sdy(m) sdz(m) vx(m/s) vy(m/s) vz(m/s) age(s) ratio
2284 109908.000 -2654301.9349 3767744.5271 4394555.9078 5 14 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.00 0.0
2284 109909.000 -2654301.9454 3767744.5016 4394555.9292 5 14 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.00 0.0
2284 109910.000 -2654301.3708 3767745.1125 4394556.4741 5 16 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.00 0.0
小程序界面:
运行pyinstaller -F -w setup.py命令,生成可执行程序:
import shutil
import re
import datetime
import os
from PyQt5.QtWidgets import QApplication, QDialog, QVBoxLayout, QLabel, QLineEdit, QPushButton, QFileDialog
class MyDialog(QDialog):
def __init__(self, parent=None):
super(MyDialog, self).__init__(parent)
self.setWindowTitle('提取pos文件:复制-匹配-裁剪')
self.setGeometry(100, 100, 500, 300)
# 创建垂直布局管理器
layout = QVBoxLayout()
# 创建两个文本输入框
label1 = QLabel('请输入起始关键字:UTC时间,2023-10-07 10:09:08', self)
layout.addWidget(label1)
self.text_input1 = QLineEdit(self)
self.text_input1.setText("2023-10-30 12:00:00")
layout.addWidget(self.text_input1)
label2 = QLabel('请输入截止关键字:', self)
layout.addWidget(label2)
self.text_input2 = QLineEdit(self)
self.text_input2.setText("2023-10-31 00:00:00")
layout.addWidget(self.text_input2)
# 创建两个文件选择框
label3 = QLabel('请选择pos原文件:', self)
layout.addWidget(label3)
self.file_input1 = QPushButton('浏览...', self)
layout.addWidget(self.file_input1)
self.file_input1.clicked.connect(self.open_file_dialog1)
label4 = QLabel('请选择保存文件的目录:', self)
layout.addWidget(label4)
self.file_input2 = QPushButton('浏览...', self)
layout.addWidget(self.file_input2)
self.file_input2.clicked.connect(self.open_file_dialog2)
# 添加确认执行按钮
self.ok_button = QPushButton('执行', self)
self.ok_button.clicked.connect(self.execute)
layout.addWidget(self.ok_button)
# 将布局设置为主窗口的布局
self.setLayout(layout)
def open_file_dialog1(self):
# 打开第一个文件选择对话框
options = QFileDialog.Options()
options |= QFileDialog.ReadOnly
fileName, _ = QFileDialog.getOpenFileName(self, '选择pos原文件', '.', '', options=options)
if fileName:
self.file_input1.setText(fileName)
def open_file_dialog2(self):
# 打开第二个文件选择对话框
options = QFileDialog.Options()
options |= QFileDialog.ReadOnly
path2 = QFileDialog.getExistingDirectory(self, '选择存放的目录', '.')
if path2:
self.file_input2.setText(path2)
def execute(self):
# 执行操作,可以在这里处理输入框和文件选择框的内容
pattern_start = self.text_input1.text()
pattern_end = self.text_input2.text()
src_path = self.file_input1.text()
dst_path = self.file_input2.text()
# 示例:将2023年10月19日12时(UTC时间)转换为GPS周和周内秒
utc_time_s = datetime.datetime.strptime(pattern_start, '%Y-%m-%d %H:%M:%S')
utc_time_e = datetime.datetime.strptime(pattern_end, '%Y-%m-%d %H:%M:%S')
# GPS周起始时间(1980年1月6日)
gps_epoch = datetime.datetime(1980, 1, 6)
# 计算时间差
time_difference_s = utc_time_s - gps_epoch
time_difference_e = utc_time_e - gps_epoch
# 计算总秒数和周数,gps时间比utc时间快了18秒
total_seconds_s = time_difference_s.total_seconds() + 18
gps_week_s = total_seconds_s // (7 * 24 * 3600)
gps_week_seconds_s = total_seconds_s % (7 * 24 * 3600)
pattern_start_ss = str(int(gps_week_s)) + " " + str(gps_week_seconds_s)
total_seconds_e = time_difference_e.total_seconds() + 18
gps_week_e = total_seconds_e // (7 * 24 * 3600)
gps_week_seconds_e = total_seconds_e % (7 * 24 * 3600)
pattern_end_ee = str(int(gps_week_e)) + " " + str(gps_week_seconds_e)
# print(f"GPS周: {gps_week_s}")
# print(f"周内秒: {gps_week_seconds_s}")
now = datetime.datetime.now()
if "PROD" in src_path:
file_name = "PROD_" + now.strftime('%Y%m%d_%H%M%S') + '.pos'
elif "TEST" in src_path:
file_name = "TEST_" + now.strftime('%Y%m%d_%H%M%S') + '.pos'
else:
print("非PROD和TEST环境的SSR文件")
file_name = now.strftime('%Y%m%d_%H%M%S') + '.pos'
shutil.copy2(src_path, dst_path + '/' + file_name)
dst_w_path = dst_path + '/' + 'N_' + file_name
with open(dst_path + '/' + file_name, 'r') as dst, open(dst_w_path, 'w') as dst_w:
# 先写入文件前两行
lineTwo = dst.readlines()[:2]
dst_w.writelines(lineTwo)
with open(dst_path + '/' + file_name, 'r') as dst, open(dst_w_path, 'a+') as dst_w:
# 再匹配
# 默认不写入文件
writing = False
num_count = 0
for line in dst:
# 正则匹配,起始行
if re.search(pattern_start_ss, line):
writing = True
if writing:
num_count += 1
dst_w.write(line)
# 正则匹配,截止行
if re.search(pattern_end_ee, line):
writing = False
break
print(r'总共%s行写入完成,起始于:%s,截止于:%s' % (num_count, pattern_start, pattern_end))
os.remove(dst_path + '/' + file_name)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
dialog = MyDialog()
dialog.exec_()