[Tutor] How to read unicode strings from a binary file and display them as plain ascii?

Kent Johnson kent37 at tds.net
Tue Mar 1 12:50:01 CET 2005


R. Alan Monroe wrote:
> I started writing a program to parse the headers of truetype fonts to
> examine their family info. But I can't manage to print out the strings
> without the zero bytes in between each character (they display as a
> black block labeled 'NUL' in Scite's output pane)
> 
> I tried:
>      stuff = f.read(nlength)
>      stuff = unicode(stuff, 'utf-8')

I think you need 'utf-16be', not 'utf-8'. See this page if you don't know the difference:
http://www.joelonsoftware.com/articles/Unicode.html

Kent

>      print type(stuff), 'stuff', stuff.encode()
>         
> This prints:
> 
>      <type 'unicode'> stuff [NUL]C[NUL]o[NUL]p[NUL]y[NUL]r[NUL]i[NUL]g[NUL]
> 
> Apparently I'm missing something simple, but I don't know what.
> 
> Alan
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 




More information about the Tutor mailing list