I am using python3 to convert a number of files from comma to | delimiter
martineau gave a very helpful answer on this topic a year ago Convert csv file to pipe delimited file in python
however, it would only work for me if i changed the file fromat from rb to rt
my code is as follows
import csv
from os import listdir
from os.path import isfile, join
z= '\\'
path = r"C:\x milestone" + z
ipath = path+ r"toconvert" +z
opath = path + r'converted' +z
onlyfiles = [f for f in listdir(ipath) if isfile(join(ipath, f))] #list of files in ipath
for each in onlyfiles:
with open( ipath + each, 'rt') as inFile:
with open(opath+ each, 'w') as outFile:
reader = csv.DictReader(inFile)
writer = csv.DictWriter(outFile, reader.fieldnames, delimiter='|')
writer.writeheader()
writer.writerows(reader)
question it insists on adding an extra linefeed after each line. is there a way to avoid this?