Wednesday, April 29, 2009

read and write UNICODE file in Python

Lately I found need to delete blank line in a text file. what's special is that the file is encoded in utf-16le, rather than ascii. After some trials, the following
script does the job nicely.


import sys
import codecs

print(sys.getdefaultencoding())
print(sys.version_info)

raw = codecs.open(r'd:/u','rb','utf16')
out = []
for line in raw:
if line.lstrip():
out.append(line)
codecs.open(r'd:/uu','w','utf16').writelines(out)
print('OK')

No comments:

Post a Comment