March 2003
Intermediate to advanced
656 pages
39h 30m
English
open
open(filename,mode='rb',encoding=None,errors=’strict',buffering=1)
Uses the built-in function open (covered in Chapter 10) to supply a file-like object that accepts
and/or provides Unicode strings to/from Python client code, while the
underlying file can either be in Unicode (when
encoding is None) or
use the codec named by encoding. For
example, if you want to write Unicode strings to file
uni.txt and have the strings implicitly encoded
as latin-1 in the file, replacing with
'?' any character that cannot be encoded in
Latin-1, use the following:
import codecs
flout = codecs.open('uni.txt','w','latin-1','replace')
# now you can write Unicode strings directly to flout
flout.write(u'élève')
flout.close( )Read now
Unlock full access