53 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			53 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| import pandas as pd
 | |
| import os
 | |
| 
 | |
| from global_var import global_var_init
 | |
| cycle, fs_ecg, fs_ppg, record_name, record_name_csv, data_path = global_var_init()
 | |
| 
 | |
| 
 | |
| # 读取文件内容
 | |
| with open(f"D://python_study//big_boss//doc//output//{record_name}//{record_name}_output.txt", 'r') as file:
 | |
|     data = file.read()
 | |
| 
 | |
| # 分割数据
 | |
| lines = data.strip().split('\n')
 | |
| 
 | |
| # 初始化列数据
 | |
| columns = [[]]  # 使用一个列表来存储多个列的数据
 | |
| current_col_index = 0
 | |
| 
 | |
| # 解析数据
 | |
| for line in lines:
 | |
|     if line.startswith('#'):
 | |
|         # 如果遇到分隔符,则切换到下一列
 | |
|         columns.append([])
 | |
|         current_col_index += 1
 | |
|     else:
 | |
|         columns[current_col_index].append(float(line))
 | |
| 
 | |
| # 找到最长列的长度
 | |
| max_length = max(len(col) for col in columns)
 | |
| 
 | |
| # 填充较短的列
 | |
| for i in range(len(columns)):
 | |
|     while len(columns[i]) < max_length:
 | |
|         columns[i].append(None)  # 使用None填充
 | |
| 
 | |
| # 创建DataFrame
 | |
| df = pd.DataFrame({f'Column{idx+1}': col for idx, col in enumerate(columns)})
 | |
| 
 | |
| # 确保目录存在
 | |
| directory = f'D://python_study//big_boss//doc//output//{record_name}'
 | |
| if not os.path.exists(directory):
 | |
|     os.makedirs(directory)
 | |
| 
 | |
| # 确定文件保存路径
 | |
| file_path = os.path.join(directory, f"{record_name}_output.xlsx")
 | |
| 
 | |
| # 保存为Excel文件
 | |
| try:
 | |
|     df.to_excel(file_path, index=False, engine='openpyxl')
 | |
|     print(f"Excel文件已生成: {file_path}")
 | |
| except Exception as e:
 | |
|     print(f"保存文件时出现错误: {e}")
 |