python_批量计算指定目录下的数据的固定率和时长

小工具:在指定的目录下,批量计算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


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

        self.initUI()

    def initUI(self):
        self.setWindowTitle('批量计算固定率')
        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('日志文件路径:')
        layout.addWidget(label1)

        self.line_edit1 = QLineEdit()
        layout.addWidget(self.line_edit1)

        label2 = QLabel('计算结果存放路径:')
        layout.addWidget(label2)

        self.line_edit2 = QLineEdit()
        layout.addWidget(self.line_edit2)

        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()
        if self.radio_button1.isChecked():
            option = 'gga'
        else:
            option = 'pos'

        self.function(input1, input2, option)

    def function(self, source_dir, target_dir, option):
        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:
                num_sum, num_all, per = 0, 0, 0
                times = 0

                if option == 'gga' and "gga" in file_path:
                    with open(file_path, 'r') as fr:
                        for lines in fr:
                            temp = lines.split(",")

                            if temp[0] == "$GPGGA":
                                # GPGGA总行数
                                num_all += 1
                                if temp[6] == '4':
                                    # 固定解=4行数
                                    num_sum += 1


                elif option == 'pos' and "pos" in file_path:
                    with open(file_path, 'r') as fr:

                        for lines in fr:
                            temp = lines.split(" ")

                            if "POS:" in temp[0]:
                                # POS总行数
                                num_all += 1
                                if "POS:1" in temp[0]:
                                    # POS:1固定解行数
                                    num_sum += 1

                if num_all >= 1:
                    # 固定率
                    per = round(num_sum / num_all * 100, 2)

                    # 数据时长/小时
                    times = round(num_all / 3600, 2)

                    sitename = os.path.basename(file_path)[:4]

                    result = sitename + ",数据时长/小时," + str(times) + ",固定率," + str(per) + "%"
                    print(result)

                    # 构造目标路径
                    t = datetime.now().strftime("%m%d%H%M")
                    target_path = os.path.join(target_dir, '固定率结果' + t + '.csv')

                    with open(target_path, 'a+') as resultfile:
                        resultfile.writelines(result + '\n')

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


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

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-18 17:26:05       76 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-18 17:26:05       81 阅读
  3. 在Django里面运行非项目文件

    2023-12-18 17:26:05       65 阅读
  4. Python语言-面向对象

    2023-12-18 17:26:05       76 阅读

热门阅读

  1. 多人协作项目代码规范统一约定(完整全面)

    2023-12-18 17:26:05       60 阅读
  2. 嵌套json 数组结构

    2023-12-18 17:26:05       54 阅读
  3. linux shell 使用mv 循环替换文件名

    2023-12-18 17:26:05       59 阅读
  4. C语言 typedef 和 #define 区别

    2023-12-18 17:26:05       64 阅读
  5. leetcode算法题:岛屿数量

    2023-12-18 17:26:05       52 阅读
  6. 经验丰富的王警官

    2023-12-18 17:26:05       61 阅读
  7. HTML面试题

    2023-12-18 17:26:05       52 阅读