I have ~1000 files with with a two-column array with varying numbers of rows having the extension .csv. I need to read each line of the file, skip over the 1st header line, and then write all the contents as a tab-delimited .txt file. I have tried to write this myself in python. I want to change numbers.csv
X,Y
1,2
3,4
...
into
1[tab]2
3[tab]4
...
My code is below.
At the "next(csv_file)" operation, my program is reading me the error "StopIteration" The contents of the infile are erased. If I remove this line from my code, I still over-write the in-file but do nothing with an out-file.
Can someone help me get this going? `
import csv
import os
cwd = os.getcwd()
#print(cwd)
for file in os.listdir():
file_name, file_ext = os.path.splitext(file)
if file_ext == '.csv':
with open(file,'r') as csv_file:
csv_reader = csv.reader(csv_file)
next(csv_file)
for line in csv_reader:
with open(file, 'w') as new_txt: #new file has .txt extension
txt_writer = csv.writer(line, delimiter = '\t') #writefile
txt_writer.writerow(line) #write the lines to file`