I want to convert a .txt file into CSV format. Here's an example of what I have in my .txt file:
0 1
1 346
5 10
Here's the code that I'm using:
import csv
txtfile = r"file.txt"
csvfile = r"file.csv"
with open(txtfile, 'r') as infile, open(csvfile, 'w') as outfile:
stripped = (line.strip() for line in infile)
lines = (line.split() for line in stripped if line)
writer = csv.writer(outfile)
writer.writerows(lines)
As a result, I get an empty line after every single line with numbers (=> all the even lines are empty): https://i.stack.imgur.com/19QAV.png. There are no empty lines in the .txt file, however.
Could someone please help me figure out why am I getting those extra empty lines in CSV file?