【问题标题】:Reading TDMS files in python_ how to use tdmsinfo command?在 python_ 中读取 TDMS 文件如何使用 tdmsinfo 命令?
【发布时间】:2017-06-28 08:09:54
【问题描述】:

我想知道 Labview 制作的 tdms 文件的内容是什么。

按照site,我用 Python 编写:

import numpy as np
from nptdms import TdmsFile
from nptdms import tdms

#read a tdms file
filenameS = "RESULTS.tdms"
tdms_file = TdmsFile(filenameS)

tdmsinfo [--properties] tdms_file

我收到以下错误:

tdmsinfo [--properties] tdms_file
                                    ^
SyntaxError: invalid syntax

我不知道如何解决它。

感谢您的帮助:)

【问题讨论】:

  • 尝试删除 [ ]。所以:tdmsinfo --properties tdms_file。请注意,它是一个命令行程序。
  • 感谢您的回复。我仍然得到同样的错误:tdmsinfo --properties tdms_file ^ SyntaxError: invalid syntax
  • 我认为是命令行程序,所以不能在程序中使用。你可以试试这样的:channel = tdms_file.object('Group', 'Channel1') data = channel.data.否则我建议你阅读手册:media.readthedocs.org/pdf/nptdms/latest/nptdms.pdf,里面有很多代码示例。
  • this channel = tdms_file.object('Group', 'Channel1') 当一个人知道对象的路径时很有用,我首先需要知道'Group'和'Channel1'的名称, ,只有这样我才能读取他们的数据。

标签: python labview


【解决方案1】:

你要找的是:

首先从文件中创建一个 TMDS 对象:

tdms_file = TdmsFile("C:\\Users\\XXXX\\Desktop\\xx Python\\XXXX.tdms")

然后获取组名:

tdms_groups = tdms_file.groups()

确定文件中包含哪些组名后,只需编写

tdms_groups

它将打印以下内容:

['Variables_1'、'Variables_2'、'Variables_3'、'Variables_4'等]

现在有了组名,您将能够获得以下频道:

tdms_Variables_1 = tdms_file.group_channels("Variables_1")

接下来将您的频道包含在该组中:

tdms_Variables_1

它会显示:

[路径为/'Variables_1'/'Channel_1'的TdmsObject,路径为/'Variables_1'/'Channel_2'的TdmsObject等]

最后得到向量及其数据:

MessageData_channel_1 = tdms_file.object('Variables_1', 'Channel_1')
MessageData_data_1 = MessageData_channel_1.data

检查您的数据

MessageData_data_1

处理您的数据! 干杯!

【讨论】:

    【解决方案2】:

    要遍历根对象的所有属性,试试这个:

     #read a tdms file
     filenameS = "RESULTS.tdms"
     tdms_file = TdmsFile(filenameS)
     root_object = tdms_file.object()
    
     # Iterate over all items in the properties dictionary and print them
     for name, value in root_object.properties.items():
          print("{0}: {1}".format(name, value))
    

    这应该给你所有的属性名称。

    【讨论】:

      【解决方案3】:

      您的问题似乎是 tdmsinfo 在 Python 脚本中不起作用,因为它不是 Python 命令:它是 "a command line program"

      解决方案是使用 Windows shell 中的“tdmsinfo”,或者在 python 中创建一个包装器,以便它在子进程中为您运行命令。例如在 Python3 中带有 subprocess 包

      import subprocess
      tdmsfile='my_file.tdms'
      # startup info to hide the windows shell
      si = subprocess.STARTUPINFO()
      si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
      #si.wShowWindow = subprocess.SW_HIDE # default
      # run tdmsinfo in a subprocess and capture the output in a
      a = subprocess.run(['tdmsinfo',tdmsfile],
                         stdout=subprocess.PIPE,
                         startupinfo=si).stdout
      a = a.decode('utf-8')
      print(a)
      

      上面的代码应该只为您提供通道和组,但您也可以使用 -p 标志运行以包含所有 TDMS 对象属性

      a = subprocess.run(['tdmsinfo','-p',tdmsfile],
                         stdout=subprocess.PIPE,
                         startupinfo=si).stdout
      

      【讨论】:

        猜你喜欢
        • 2014-05-22
        • 2018-06-11
        • 1970-01-01
        • 1970-01-01
        • 2018-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-31
        相关资源
        最近更新 更多