简体   繁体   English

在python_中读取TDMS文件如何使用tdmsinfo命令?

[英]Reading TDMS files in python_ how to use tdmsinfo command?

I would like to know what is the content of a tdms file, which is produced by Labview. 我想知道Labview产生的tdms文件的内容是什么。

Following this site , I write in Python: 在这个站点之后 ,我用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

I receive the following error: 我收到以下错误:

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

I do not how to fix it. 我不怎么解决它。

Thank you for your help :) 谢谢您的帮助 :)

What you are looking for is: 您正在寻找的是:

First create a TMDS objet from file: 首先从文件创建TMDS对象:

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

then get the group names with: 然后使用以下命令获取组名:

tdms_groups = tdms_file.groups()

after you can figure out which group names you have into the file, just write 在找出文件中包含哪些组名后,只需编写

tdms_groups

It will print the following: 它将打印以下内容:

['Variables_1', 'Variables_2', 'Variables_3', 'Variables_4', etc..] ['Variables_1','Variables_2','Variables_3','Variables_4'等。]

With group names now u will be able to get channels with the following: 现在有了组名,您将能够获得具有以下内容的频道:

tdms_Variables_1 = tdms_file.group_channels("Variables_1")

Next print your channels contain into that group: 接下来将您的频道包含在该组中:

tdms_Variables_1

It will show: 它会显示:

[ TdmsObject with path /'Variables_1'/'Channel_1', TdmsObject with path /'Variables_1'/'Channel_2', etc..] [具有路径/'Variables_1'/'Channel_1'的TdmsObject,具有路径/'Variables_1'/'Channel_2'的TdmsObject,等等。]

At the end get the vectors and its data: 最后获得向量及其数据:

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

Check your data 检查你的数据

MessageData_data_1

do stuff with your data! 用您的数据做事! cheers! 干杯!

To loop over all properties from the root object try this: 要遍历根对象的所有属性,请尝试以下操作:

 #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))

That should give you all properties names. 那应该给您所有属性名称。

Your problem seems to be that tdmsinfo will not work inside a Python script as it is not a python command: it's "a command line program" . 您的问题似乎是tdmsinfo无法在Python脚本内运行,因为它不是python命令:它是“命令行程序”

The solution is to either use 'tdmsinfo' from a windows shell, or make a wrapper in python so that it runs the command in a subprocess for you. 解决方案是使用Windows外壳程序中的“ tdmsinfo”,或在python中进行包装,以便它在您的子进程中运行命令。 For instance in Python3 with the subprocess package 例如在带有子流程包的Python3中

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)

the code above should give you only the channels and groups, but you can also run with the -p flag to include all the TDMS object properties 上面的代码应该只为您提供通道和组,但是您也可以使用-p标志运行以包含所有TDMS对象属性

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM