[Tutor] Re: Not-quite-unicode string, how to convert to ascii?

Jeff Kowalczyk jtk at yahoo.com
Mon Apr 19 13:49:37 EDT 2004


I figured it out, i needed to chain a decoding and encoding:

>>> f = open('input.txt','r')
>>> s = f.read()
>>> s[:20]
' \x00 \x00B\x00a\x00t\x00c\x00h\x00 \x00 \x00 \x00'
>>> s[:20].decode('utf-16')
u'  Batch   '
>>> s[:20].decode('utf-16').encode('ascii')
'  Batch   '





More information about the Tutor mailing list