Skip to content Skip to sidebar Skip to footer

How To Convert Csv File To Text File Using Python?

I want to convert a couple of .csv files to .txt files using python. In my .csv files, I have hundreds of lines of data like the bellow: image of the csv file Value Date Time

Solution 1:

Using csv it's very easy to iterate over the csv lines:

import csv
csv_file = raw_input('Enter the name of your input file: ')
txt_file = raw_input('Enter the name of your output file: ')
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()

Post a Comment for "How To Convert Csv File To Text File Using Python?"