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

2 comments:

Michael Tobis said...

No idea why this is double spaced.

Michael Tobis said...

It works if you just paste it despite the extra line feeds.

Be sure to keep the indentation of the lines after "if" and "else" and make sure they are indented by the same number of spaces.