Read TDMS file with Python module npTDMS

Python code example using module npTDMS to read a National Instruments TDMS data file.  

ChnExamples_TDMS_file.zip
# npTDMS
#
#	This script updated Apr 2022
#
# npTDMS documentation:
# https://pypi.org/project/npTDMS/
# https://nptdms.readthedocs.io/en/latest/

# Read National Instruments TDMS data files using Python.

# License:
# OSI Approved :: GNU Library or Lesser General Public License (LGPL)
# https://pypi.org/policy/terms-of-use/

# Installation:
# pip install npTDMS

# Example written by:  Mark Kiehl
# http://mechatronicsolutionsllc.com/
# http://www.savvysolutions.info/savvycodesolutions/
# http://www.savvydiademsolutions.com/

def showRootProps(oTdmsFile):
    """Print the file properties
    """
    # method .properties returns a dictionary
    root_props = oTdmsFile.properties.items()
    print ('File properties:')
    for key,value in root_props:
        print ("\t{0} : {1} ".format(key,value))

def showGroups(oTdmsFile):
    liGrps = oTdmsFile.groups()
    #liGrps is a list of strings
    print ('Channel Groups:')
    for idx, sGrp in enumerate(liGrps, start=0):
        print('\t{} {}'.format(idx, sGrp))


from nptdms import TdmsFile

#Read the TDMS file
sFilePath = 'D:\Documents\my_data\computer\Python\py_reads_TDMS_nptdms\ChnExamples.tdms'
oTDMS = TdmsFile(sFilePath)

showRootProps(oTDMS)
print('\n')
#Get a single file property
dteFilePropDateTime = oTDMS.properties['datetime']
print ('File "datetime" = {}'.format(dteFilePropDateTime))
print ('File "description" = {}'.format(oTDMS.properties['description']))
print('\n')

#Show all of the channel groups names
showGroups(oTDMS)
print ('\n')

# Get a particular channel group
oGrp = oTDMS['Explicit chn types']
print ('oGrp.name: "{}" '.format(oGrp.name))
print ('\n')

#Get a list of channel objects in oGrp
liChnsObj = oGrp.channels()
print ('channel group "{}" channels:'.format(oGrp.name))
for idx, oChn in enumerate(liChnsObj, start=0):
    print ('{} {}'.format(idx, oChn.name))
print ('\n')

#Get a time channel and data channel
sChnTime = liChnsObj[0].name
sChnData = liChnsObj[2].name
oChnTime = oGrp[sChnTime]
oChnData = oGrp[sChnData]
print ('"{} / {}"  size:{}'.format(oGrp.name, oChnTime.name, len(oChnTime)))
print ('"{} / {}"  size:{}'.format(oGrp.name, oChnData.name, len(oChnData)))
print ('\n')

#Iterate over the properties for channel oChnData
print ('"{} / {}" properties:'.format(oGrp.name, oChnData.name))
for key,value in oChnData.properties.items():
	print ("\t{0} : {1} ".format(key,value))
print ('\n')



##	===========================================================================
##
##	MIT License
##	
##	Copyright (c) 2018,2019,2020,2021 Mechatronic Solutions LLC  (www.http:##mechatronicsolutionsllc.com)
##	
##	Permission is hereby granted, free of charge, to any person obtaining a copy
##	of this software and associated documentation files (the "Software"), to deal
##	in the Software without restriction, including without limitation the rights
##	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
##	copies of the Software, and to permit persons to whom the Software is
##	furnished to do so, subject to the following conditions:
##	
##	The above copyright notice and this permission notice shall be included in all
##	copies or substantial portions of the Software.
##
##	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
##	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
##	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
##	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
##	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
##	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
##	THE SOFTWARE.		
##	===========================================================================