-1

I am trying to convert the data here which I put in a text file to convert into CSV file.

This is the code:

import csv

txt_file = r"TestData1.txt"
csv_file = r"TestData1.csv"

in_txt = csv.reader(open(txt_file, "rt"), delimiter = '\t')
out_csv = csv.writer(open(csv_file, 'wt'))

out_csv.writerows(in_txt)

It works but has a empty line every other line. How do I go fix that?

as35fgh
  • 1
  • 1

1 Answers1

0

This question has already been addressed here.

In Python 3.x, it turns out that you can specify how you want to process the new line by adding newline='' (for no newline character).

import csv

txt_file = r"TestData1.txt"
csv_file = r"TestData1.csv"

in_txt = csv.reader(open(txt_file, "rt"), delimiter = '\t')
out_csv = csv.writer(open(csv_file, 'wt', newline=''))


out_csv.writerows(in_txt)
Taylor Iserman
  • 175
  • 1
  • 16