1

The raw ECG that I have is in csv format. I need to convert it into .txt file which will have only the ECG data. I need a python code for the same. Can I get some help on this.

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()

The input ECG data looks like this: Raw_ECG_data

Cœur
  • 37,241
  • 25
  • 195
  • 267
Jasmine
  • 476
  • 3
  • 22
  • Try Following this link https://stackoverflow.com/questions/47339698/how-to-convert-csv-file-to-text-file-using-python – Rohit Maharashi Nov 19 '19 at 10:16
  • 1
    `.csv` file is actually a `.txt` file. just rename. I think what you want is to select and filter a column in the `.csv` file. – MEdwin Nov 19 '19 at 10:20
  • I had seen that link. join includes both the data. I check the python documentation, I did'nt find a suitable function to include only specific data. – Jasmine Nov 19 '19 at 11:00
  • @MEdwin, yes thats exactly what I want to do. – Jasmine Nov 19 '19 at 11:01
  • Okay, it is already answered below. You can basically use @asif's code and change the part with `join(row)` to `join(row[2])`. It means you are filtering column 3 from the original file into the output `txt` file. Let me know if it works. – MEdwin Nov 19 '19 at 11:07

2 Answers2

3

What worked for me

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()
Mahesh Kumaran
  • 887
  • 2
  • 12
  • 30
Asif
  • 171
  • 10
  • I tried the code given by @Asif. join(row[2]) results in a out of index error. join(row[1]) results in blank .txt file. While join(row[0]) gives the unwanted column data. – Jasmine Nov 19 '19 at 11:26
  • @Jasmine it should work if the input csv is same as you specified. can you upload your csv and specify the exact output you want from it? it will be easy to help – Asif Nov 19 '19 at 12:18
  • [![The required .txt files][1]][1], this is the required output [![The raw ECG data][2]][2] [1]: https://i.stack.imgur.com/eOz94.png [2]: https://i.stack.imgur.com/CWJus.png – Jasmine Nov 20 '19 at 04:41
1

A few things:

  • You can open multiple files with the same context manager (with statement):
with open(csv_file, 'r') as input_file, open(txt_file, 'w') as output_file:
    ...
  • When using a context manager to handle files, there's no need to close the file, that's what the with statement is doing; it's saying "with the file open, do the following". So once the block is ended, the file is closed.

  • You could do something like:

with open(csv_file, 'r') as input_file, open(txt_file, 'w') as output_file:
    for line in input_file:
        output_file.write(line)

... But as @MEdwin says a csv can just be renamed and the commas will no longer act as separators; it will just become a normal .txt file. You can rename a file in python using os.rename():

import os

os.rename('file,txt', 'file.csv')
  • Finally, if you want to remove certain columns from the csv when writing to the txt file, you can use .split(). This allows you use an identifier such as a comma, and separate the line according this identifier into a list of strings. For example:
"Hello, this is a test".split(',')
>>> ["Hello", "this is a test"]

You can then just write certain indices from the list to the new file.

For more info on deleting columns en masse, see this post

QuantumChris
  • 963
  • 10
  • 21