Lukas00990 2021-07-18 19:12 采纳率: 40.8%
浏览 356
已结题

Python 读取 TDMS 文件

专家好,

想咨询如何用Python处理TDMS 文件,并将其成功转换为EXCEL 格式的数据。 我网上看了这样一个代码

import xlwt
import codecs
import os

"""
├─tdms_scripts
│  ├─excel
│  └─tdms
│──tmds2excel.py
"""

# 改为你的tmds文件夹!的路径,比如0静止
root_path = r'C:\Users\'
# 修改这里,第0个为1,第1个从1001开始
task_num = 0
# 创建一个excel文件夹
try:
    os.mkdir(os.path.join(root_path, 'excel' + str(task_num)))
except:
    pass
tdms_files = os.listdir(os.path.join(root_path,'tdms' ))
# 将文件名从小到大排序
tdms_files.sort()
# [print(t) for t in tdms_files]

file_count = 1 + task_num * 1000
for tdms_file in tdms_files:
    print("tdms_file:", tdms_file, end='\t')
    sheetName = 'Sheet1'
    start_row = 0
    start_col = 0

    wb = xlwt.Workbook(encoding='utf-8')
    ws = wb.add_sheet(sheetName)
    tdms_path = os.path.join(root_path, 'tdms', tdms_file)
    f = open(tdms_path, encoding='utf-8')

    row_excel = start_row
    for line in f:
        # 消除换行符
        line = line.strip('\n')
        # 以制表符'\t'作为切分对象,将长字符串,转为短字符串列表;
        # 如果原数据用空格切分,则line = line.split(' ')
        line = line.split('\t')

        col_excel = start_col
        len_line = len(line)
        for j in range(len_line):
            ws.write(row_excel, col_excel, line[j])
            col_excel += 1
            excel_path = os.path.join(root_path, 'excel' + str(task_num), str(file_count) + '.xls')
            # 如果用原名,用下面的句子
            # excel_path = os.path.join(root_path, 'excel', tdms_file.replace('tdms', 'xls'))
            wb.save(excel_path)

        row_excel += 1

    f.close
    print("excel_path:", excel_path)
    file_count += 1

但是会报错
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

根据网上的提示,我把utf-8 改成了utf-16, 又出现了这样的错误
UnicodeError: UTF-16 stream does not start with BOM

不知专家有知道问题出到哪儿了吗?或者说专家有更好的处理TDMS 文件的办法?

  • 写回答

2条回答 默认 最新

  • Soda Wang 2021-07-18 20:45
    关注

    这种错误可能是编码的问题!可是使用chardet检测编码。或者尝试各种编码:

    #PYTHON
    encodings = ['utf-8', 'windows-1250', 'windows-1252'] # add more
                for e in encodings:
                    try:
                        fh = codecs.open('file.txt', 'r', encoding=e)
                        fh.readlines()
                        fh.seek(0)
                    except UnicodeDecodeError:
                        print('got unicode error with %s , trying different encoding' % e)
                    else:
                        print('opening the file with encoding:  %s ' % e)
                        break       
    

    另外,好像有专门将TDMS文件导入到Excel的软件:

    img
    这种文件好像是这种格式,读取起来大概不困难吧

    img
    具体可以参考网站:The NI TDMS File Format

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 7月31日
  • 已采纳回答 7月23日
  • 创建了问题 7月18日

悬赏问题

  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型