Sunday, December 12, 2010

python - text replace line endings - decode and encode

import os
import sys


#replaces text or line endings (data holds the opened and read file) (try except etc. removed)
f = open(afile, 'r').read()
data = f.decode('iso-8859-1')
data = data.replace('\r\n','\n')
data = data.replace('\r','\n')


#write to file
f = open(afile, 'w')
f.write(data.encode('utf-8'))
f.close()

No comments:

Post a Comment