Name
open
Synopsis
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( )
Get Python in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.