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_())

  • 12
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值