python_批量筛选指定目录下的gga&pos文件

发布时间:2023年12月20日

小工具:在指定的目录下,批量筛选出符合要求的gga或者pos文件。

import sys
import os
from datetime import datetime
from PyQt5.QtWidgets import QApplication, QDialog, QVBoxLayout, QLabel, QLineEdit, QPushButton, QRadioButton
from PyQt5.QtCore import Qt
import time
import re


class InputDialog(QDialog):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        self.setWindowTitle('批量筛选出匹配的gga&pos数据')
        self.setGeometry(600, 600, 600, 200)
        self.setWindowFlags(Qt.Dialog | Qt.WindowMinMaxButtonsHint | Qt.WindowCloseButtonHint)

        layout = QVBoxLayout()

        # self.radio_button1 = QRadioButton('gga', self)
        # self.radio_button2 = QRadioButton('pos', self)
        # self.radio_button1.setChecked(True)
        # layout.addWidget(self.radio_button1)
        # layout.addWidget(self.radio_button2)

        label1 = QLabel('gga起始关键字,如090511.00,15,12,2023')
        layout.addWidget(label1)

        self.line_edit1 = QLineEdit()
        self.line_edit1.setText("105441.00,13,12,2023")
        layout.addWidget(self.line_edit1)

        label2 = QLabel('gga截止关键字')
        layout.addWidget(label2)

        self.line_edit2 = QLineEdit()
        self.line_edit2.setText("115441.00,13,12,2023")
        layout.addWidget(self.line_edit2)

        label3 = QLabel('pos起始关键字,如2023/12/15 09:05:29')
        layout.addWidget(label3)

        self.line_edit3 = QLineEdit()
        self.line_edit3.setText("2023/12/13 10:55:12")
        layout.addWidget(self.line_edit3)

        label4 = QLabel('gga截止关键字')
        layout.addWidget(label4)

        self.line_edit4 = QLineEdit()
        self.line_edit4.setText("2023/12/13 11:55:12")
        layout.addWidget(self.line_edit4)

        label5 = QLabel('日志文件路径:如,E:\data\\fortest\日志文件')
        layout.addWidget(label5)

        self.line_edit5 = QLineEdit()
        self.line_edit5.setText(r'E:\data\fortest\日志文件')
        layout.addWidget(self.line_edit5)

        label6 = QLabel('结果存放路径:')
        layout.addWidget(label6)

        self.line_edit6 = QLineEdit()
        self.line_edit6.setText(r'E:\data\fortest\日志文件')
        layout.addWidget(self.line_edit6)

        button = QPushButton('提取')
        button.clicked.connect(self.submit)
        layout.addWidget(button)

        self.setLayout(layout)

    def submit(self):
        input1 = self.line_edit1.text()
        input2 = self.line_edit2.text()
        input3 = self.line_edit3.text()
        input4 = self.line_edit4.text()
        input5 = self.line_edit5.text()
        input6 = self.line_edit6.text()
        # if self.radio_button1.isChecked():
        #     option = 'gga'
        # else:
        #     option = 'pos'

        # self.function(input1, input2, option)
        self.function(input1, input2, input3, input4, input5, input6)

    def function(self, gga_start, gga_end, pos_start, pos_end, source_dir, target_dir):
        for filename in os.listdir(source_dir):
            # 构造文件的完整路径
            file_path = os.path.join(source_dir, filename)

            # 如果是文件,则计算
            if os.path.isfile(file_path):  # and "gga" in file_path:

                if 'gga' in file_path and gga_start:
                    # 筛选时间段1的数据
                    sun_file_name = os.path.basename(file_path)[:-5] + ".txt"
                    dest_file_name = os.path.join(target_dir, sun_file_name)
                    self.split_file(gga_start, gga_end, file_path, dest_file_name)

                    # time.sleep(3)

                    # 筛选时间段2的数据
                    # sat_file_name = os.path.basename(target_path)[:-5] + "_sat.txt"
                    # dest_file_name = os.path.join(dest_dir, sat_file_name)
                    # split_file("102306.00,30,11,2023", "103306.00,30,11,2023", target_path, dest_file_name)

                # pos文件为gpst时间,相对utc时间快18秒
                elif 'pos' in file_path and pos_start:
                    # 筛选时间段1的数据
                    sun_file_name = os.path.basename(file_path)[:-5] + ".txt"
                    dest_file_name = os.path.join(target_dir, sun_file_name)
                    self.split_file(pos_start, pos_end, file_path, dest_file_name)

                    # time.sleep(2)

            # 如果是目录,则递归处理子目录下的文件
            elif os.path.isdir(file_path):
                self.function(gga_start, gga_end, pos_start, pos_end, file_path, target_dir)

    def split_file(self, pattern_s, pattern_e, source_files, dest_files):
        if 'gga' in source_files:
            with open(source_files, 'r') as sf, open(dest_files, 'a+') as df:
                write = False
                count_num = 0
                for line in sf:
                    if re.search(pattern_s, line):
                        write = True
                    if write:
                        count_num += 1
                        df.write(line)
                    if re.search(pattern_e, line):
                        write = False

                if count_num == 0:
                    print(r'当前时间:%s,%s没有数据匹配' % (
                        datetime.now().strftime('%Y-%m-%d %H:%M:%S'), source_files))
                else:
                    print(r'当前时间:%s,%s总共%s行写入完成,起始于:%s,截止于:%s' % (
                        datetime.now().strftime('%Y-%m-%d %H:%M:%S'), source_files, count_num, pattern_s,
                        pattern_e))

        elif 'pos' in source_files:
            with open(source_files, 'r') as sf, open(dest_files, 'w') as ds:
                # 先写入文件前两行
                lineTwo = sf.readlines()[:2]
                ds.writelines(lineTwo)
            with open(source_files, 'r') as sf, open(dest_files, 'a+') as df:
                write = False
                count_num = 0
                for line in sf:
                    if re.search(pattern_s, line):
                        write = True
                    if write:
                        count_num += 1
                        df.write(line)
                    if re.search(pattern_e, line):
                        write = False

                if count_num == 0:
                    print(r'当前时间:%s,%s没有数据匹配' % (
                        datetime.now().strftime('%Y-%m-%d %H:%M:%S'), source_files))
                else:
                    print(r'当前时间:%s,%s总共%s行写入完成,起始于:%s,截止于:%s' % (
                        datetime.now().strftime('%Y-%m-%d %H:%M:%S'), source_files, count_num, pattern_s,
                        pattern_e))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    dialog = InputDialog()
    dialog.show()
    sys.exit(app.exec_())

文章来源:https://blog.csdn.net/ramsey17/article/details/135079863
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。