我的原始心电图是csv格式的。我需要把它转换成.txt文件,只有心电数据。我需要一个相同的python代码。能帮我个忙吗。
csv_file = 'ECG_data_125Hz_Simulator_Patch_Normal_Sinus.csv'
txt_file = 'ECG_data_125Hz_Simulator_Patch_Normal_Sinus.txt'
import csv
with open(txt_file, "w") as my_output_file:
with open(csv_file, "r") as my_input_file:
//need to write data to the output file
my_output_file.close()
输入的心电图数据如下:数据
发布于 2019-11-19 10:30:24
什么对我起作用了
import csv
csv_file = 'FL_insurance_sample.csv'
txt_file = 'ECG_data_125Hz_Simulator_Patch_Normal_Sinus.txt'
with open(txt_file, "w") as my_output_file:
with open(csv_file, "r") as my_input_file:
[ my_output_file.write(" ".join(row)+'\n') for row in csv.reader(my_input_file)]
my_output_file.close()
发布于 2019-11-19 10:31:20
有几件事:
with
语句)打开多个文件:with open(csv_file, 'r') as input_file, open(txt_file, 'w') as output_file:
...
with
语句所做的;它的意思是“打开文件时,执行以下操作”。因此,一旦块结束,文件就关闭了。with open(csv_file, 'r') as input_file, open(txt_file, 'w') as output_file:
for line in input_file:
output_file.write(line)
..。但正如@MEdwin所说,csv可以重新命名,逗号不再充当分隔符;它将成为一个普通的.txt文件。可以使用os.rename()
在python中重命名文件。
import os
os.rename('file,txt', 'file.csv')
.split()
。这允许您使用诸如逗号之类的标识符,并将根据此标识符的行分隔为字符串列表。例如:"Hello, this is a test".split(',')
>>> ["Hello", "this is a test"]
然后,只需将某些索引从列表中写入新文件即可。
有关删除所有列的更多信息,见这篇文章
https://stackoverflow.com/questions/58931469
复制相似问题