Use PyQt5 to generate an executable small program: match the content in GGA format from the start keyword to the end keyword range, support multiple selection of files, and clear the copied files generated during the process.
The GGA file is as follows:
$GPZDA,063052.00,16,10,2023,,*61
$GPGGA,063052.00,4349.7377413,N,12509.8354912,E,4,40,0.6,222.928,M,0.00,M,01,2445*69
$GPZDA,063053.00,16,10,2023,,*60
$GPGGA,063053.00,4349.7377412,N,12509.8354914,E,4,40,0.6,222.926,M,0.00,M,01,2445*61
$GPZDA,063054.00,16,10,2023,,*67
Mini program interface:
Run pyinstaller -F -w setup.py -n Pytext 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 GGA files: 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: such as hour, minute and second 063055, or 063053.00,16,10,2023', self) layout.addWidget(label1) self.text_input1 = QLineEdit(self) self.text_input1.setText("063053.00,16,10,2023") 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("070434.00,16,10,2023") layout.addWidget(self.text_input2) #Create two file selection boxes label3 = QLabel('Please select the original GGA file: multiple selections are allowed', 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): file_dialog = QFileDialog(self) file_dialog.setFileMode(QFileDialog.ExistingFiles) if file_dialog.exec_(): file_paths = file_dialog.selectedFiles() if len(file_paths) > 1: # Get the directory and file name of the first file, used to create the merged file name directory = os.path.dirname(file_paths[0]) filename = os.path.splitext(os.path.basename(file_paths[0]))[0] merged_filename = os.path.join(directory, f'{filename}_merged').replace('\', '/') with open(merged_filename, 'w') as merged_file: for path in file_paths: with open(path, 'r') as file: merged_file.write(file.read()) merged_file.write('\\ ') # Add newlines between each file elif len(file_paths) == 1: merged_filename = file_paths else: print('Please select files.') if merged_filename: self.file_input1.setText(''.join(merged_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() now = datetime.datetime.now() if ("PROD" in src_path) and ("NRTK" in src_path): file_name = "PROD_NRTK_" + now.strftime('%Y%m%d_%H%M%S') elif ("TEST" in src_path) and ("NRTK" in src_path): file_name = "TEST_NRTK_" + now.strftime('%Y%m%d_%H%M%S') elif "PROD" in src_path: file_name = "PROD_" + now.strftime('%Y%m%d_%H%M%S') elif "TEST" in src_path: file_name = "TEST_" + now.strftime('%Y%m%d_%H%M%S') else: print("NRTK and SSR2OSR files for non-PROD and TEST environments") file_name = now.strftime('%Y%m%d_%H%M%S') shutil.copy2(src_path, dst_path + '/' + file_name) dst_w_path = dst_path + '/' + file_name + 'D' with open(dst_path + '/' + file_name, 'r') as dst, open(dst_w_path, 'w') as dst_w: #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, line) and line.startswith('$GPZDA'): writing=True if writing: num_count + = 1 dst_w.write(line) # Regular matching, cut off lines if re.search(pattern_end, line): # and line.startswith('$GPGGA'): 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)) if 'merged' in src_path: os.remove(src_path) os.remove(dst_path + '/' + file_name) else: os.remove(dst_path + '/' + file_name) if __name__ == '__main__': importsys app = QApplication(sys.argv) dialog = MyDialog() dialog.exec_()