专家好,
想咨询如何用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 文件的办法?