python-matching continuous content based on keywords Ⅱ

Use PyQt5 to generate an executable small program: match the contents of the pos file between the start keyword and the end keyword, convert UTC time to GPS time, and clear the copied files generated in the process.

pos file is as follows:

% (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

Mini program interface:

Run the pyinstaller -F -w setup.py command to generate an executable program:
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('Extract pos file: copy-match-crop')
        self.setGeometry(100, 100, 500, 300)

        #Create a vertical layout manager
        layout = QVBoxLayout()

        #Create two text input boxes
        label1 = QLabel('Please enter the starting keyword: UTC time, 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('Please enter the cut-off keyword:', self)
        layout.addWidget(label2)
        self.text_input2 = QLineEdit(self)
        self.text_input2.setText("2023-10-31 00:00:00")
        layout.addWidget(self.text_input2)

        #Create two file selection boxes
        label3 = QLabel('Please select the pos original file:', self)
        layout.addWidget(label3)
        self.file_input1 = QPushButton('Browse...', self)
        layout.addWidget(self.file_input1)
        self.file_input1.clicked.connect(self.open_file_dialog1)

        label4 = QLabel('Please select the directory to save the file:', self)
        layout.addWidget(label4)
        self.file_input2 = QPushButton('Browse...', self)
        layout.addWidget(self.file_input2)
        self.file_input2.clicked.connect(self.open_file_dialog2)

        #Add confirmation execution button
        self.ok_button = QPushButton('execute', self)
        self.ok_button.clicked.connect(self.execute)
        layout.addWidget(self.ok_button)

        # Set the layout to the layout of the main window
        self.setLayout(layout)

    def open_file_dialog1(self):
        # Open the first file selection dialog
        options = QFileDialog.Options()
        options |= QFileDialog.ReadOnly
        fileName, _ = QFileDialog.getOpenFileName(self, 'Select pos original file', '.', '', options=options)
        if fileName:
            self.file_input1.setText(fileName)

    def open_file_dialog2(self):
        # Open the second file selection dialog
        options = QFileDialog.Options()
        options |= QFileDialog.ReadOnly
        path2 = QFileDialog.getExistingDirectory(self, 'Select the directory to store', '.')
        if path2:
            self.file_input2.setText(path2)


    def execute(self):
        # Perform operations, you can process the contents of the input box and file selection box here
        pattern_start = self.text_input1.text()
        pattern_end = self.text_input2.text()
        src_path = self.file_input1.text()
        dst_path = self.file_input2.text()

        # Example: Convert 12:00 on October 19, 2023 (UTC time) to GPS weeks and seconds within the week
        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 week start time (January 6, 1980)
        gps_epoch = datetime.datetime(1980, 1, 6)

        # Calculate time difference
        time_difference_s = utc_time_s - gps_epoch
        time_difference_e = utc_time_e - gps_epoch

        # Calculate the total number of seconds and weeks. GPS time is 18 seconds faster than UTC time.
        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 week: {gps_week_s}")
        # print(f"Seconds during the week: {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("SSR files for non-PROD and TEST environments")
            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:
            # Write the first two lines of the file first
            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:
            # Match again
            #Do not write to file by default
            writing=False
            num_count = 0
            for line in dst:
                # Regular match, starting line
                if re.search(pattern_start_ss, line):
                    writing=True
                if writing:
                    num_count + = 1
                    dst_w.write(line)
                # Regular matching, cut off lines
                if re.search(pattern_end_ee, line):
                    writing=False
                    break
            print(r' A total of %s lines have been written, starting from: %s and ending at: %s' % (num_count, pattern_start, pattern_end))

        os.remove(dst_path + '/' + file_name)


if __name__ == '__main__':
    importsys

    app = QApplication(sys.argv)
    dialog = MyDialog()
    dialog.exec_()

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Python entry skill treeHomepageOverview 385231 people are learning the system