Friday, August 12, 2011

Fixing Excel text exports in OS X

Excel exports .tsv and .csv with a broken linefeed character.

To fix: copy this file to "fixcel.py"


# fixcel.py

from sys import argv
infnam, outfnam = argv[1:]
inf = file(infnam)
indata = inf.read()
inf.close()
outdata = ""
for ch in indata:
if ord(ch) == 13:
outdata += "\n"
else:
outdata += ch
outf = file(outfnam,"w")
outf.write(outdata)
outf.close()
#

Then invoke with:

python fixcel.py oldfilename newfilename