Reputation: 3
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`
Upvotes: 0
Views: 6367
Reputation: 386
You were on the right track. I made some changes to your code:
import csv
import os
cwd = os.getcwd()
#print(cwd)
for file in os.listdir('.'): # use the directory name here
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)
csv_reader.next() ## skip one line (the first one)
newfile = file + '.txt'
for line in csv_reader:
with open(newfile, 'a') as new_txt: #new file has .txt extn
txt_writer = csv.writer(new_txt, delimiter = '\t') #writefile
txt_writer.writerow(line) #write the lines to file`
In writing, you need to use 'a' (append) instead of 'w' (write) , otherwise, you'll just get one line - the last line. And, if that last line was a blank line, all you'll have is a file with blanks, i.e., nothing.
Upvotes: 3
Reputation: 3372
In[2]: import csv
In[3]: with open('test_file.txt', 'r') as f:
...: for line in f:
...: print(line)
...:
X,Y
1,2
3,4
In[4]: with open('test_file.txt', 'r') as f:
...: reader = csv.DictReader(f)
...: fieldnames = reader.fieldnames
...: result = list(reader)
...:
...: with open('test_output.tsv', 'w') as f:
...: writer = csv.DictWriter(f, fieldnames=fieldnames, delimiter='\t')
...: writer.writeheader() # remove this line if you don't want header
...: writer.writerows(result)
...:
In[5]: with open('test_output.tsv', 'r') as f:
...: for line in f:
...: print(line)
...:
X Y
1 2
3 4
Upvotes: 0