使用Python读取LabVIEW TDMS 格式文件转成 pandas及Excel

个人用Django开发的博客已上线,欢迎访问:https://www.zhibibin.com

 

Labview的TMDS格式文件在很多偏硬件相关的工作中经常出现,有时候面临将TDMS转成Excel通用格式的情况,正常来讲Excel只要装有TDMS插件读取是没有问题的,然而面对大量TDMS文件的时候,手动一个个将TDMS文件转成Excel文件显然很浪费时间,以下介绍使用Python配合pandas,xlsxwriter将TMDS文件批量转成Excel文件的方法:

前期需要安装以下库:

nptdms
pandas
xlsxwriter
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import os
import sys
from nptdms import TdmsFile
import pandas as pd

class Application(QWidget):
    def __init__(self):
        super().__init__()
        self.title = 'TDMS_to_Excel'
##        self.setGeometry(100,100,200,200)
        self.initGui()

    def initGui(self):
        layout = QVBoxLayout()
        
        self.Button = QPushButton('Run', self)
        self.Button.clicked.connect(self.on_click)

        layout.addWidget(self.Button)

        self.setLayout(layout)

    @pyqtSlot()
    def on_click(self):
        files = os.listdir('tdms/')

        for file in files:
            print(file)
            #load the tdms file
            tdms_file = TdmsFile(f'tdms/{file}')

            #split all the tdms grouped channels to a separate dataframe

            #tdms_file.as_dataframe()
            for group in tdms_file.groups():
                grp1_data = tdms_file.object('HEX').as_dataframe()
                grp2_data = tdms_file.object('DEC').as_dataframe()

            excel_file = file.replace('tdms', 'xlsx')
            print(excel_file)                     
            writer = pd.ExcelWriter(f"excel/{excel_file}", engine='xlsxwriter')
            grp1_data.to_excel(writer, sheet_name='HEX')
            grp2_data.to_excel(writer, sheet_name='DEC')

            # Close the Pandas Excel writer and output the Excel file.
            writer.save()

        print("complete")
      


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

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值