Unicode - I/O

codecs module

  • Provides convenient interface for encoding, decoding, and file I/O
  • General idea: perform a codec lookup:
     import codecs
     encoder, decoder, reader, writer = codecs.lookup('utf-8') 
  • Returns a tuple of functions.
  • encoder/decoder functions are low-level functions that work with partial data
  • reader/writer functions provide wrappers around other file objects.
  • Example: reading a file
     f = reader(open("foo"))     
     u = f.read()
     f.close()

In practice

  • Data encoding method is often embedded in the file itself.
  • Based on encoding method, a codec would be selected to perform the actual reading and writing
<<< O'Reilly OSCON 2001, New Features in Python 2, Slide 56
July 26, 2001, beazley@cs.uchicago.edu
>>>