What is the correct way to port codecs.open to python 3.1?
Antoine Pitrou
solipsis at pitrou.net
Sat Nov 7 13:10:24 EST 2009
Le Sat, 07 Nov 2009 12:56:54 +0100, Baptiste Lepilleur a écrit :
>
> After applying 2to3.py to port a 2.6 script to 3.1, I get the following
> error when running my script:
> File "purekeyworddbtest.py", line 143, in __init__
> f = codecs.open(EXCLUDED_KEYWORDS_FILE, 'rt', 'utf-8')
> File "c:\Python31\lib\codecs.py", line 870, in open
> file = builtins.open(filename, mode, buffering)
> ValueError: can't have text and binary mode at once
I would suggest not using codecs.open() in 3.x, since the built-in open()
will do the same thing, but faster.
So just write:
f = open(EXCLUDED_KEYWORDS_FILE, 'r', encoding='utf-8')
and you'll get a fast file object giving you str (unicode) objects after
an implicit utf-8 decoding of file data.
Regards
Antoine.
More information about the Python-list
mailing list