Difference between open and codecs.open in Python

前端 未结 8 502
一整个雨季
一整个雨季 2020-12-04 09:53

There are two ways to open a text file in Python:

f = open(filename)

And

import codecs
f = codecs.open(filename, encoding=\         


        
8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-04 10:06

    Personally, I always use codecs.open unless there's a clear identified need to use open**. The reason is that there's been so many times when I've been bitten by having utf-8 input sneak into my programs. "Oh, I just know it'll always be ascii" tends to be an assumption that gets broken often.

    Assuming 'utf-8' as the default encoding tends to be a safer default choice in my experience, since ASCII can be treated as UTF-8, but the converse is not true. And in those cases when I truly do know that the input is ASCII, then I still do codecs.open as I'm a firm believer in "explicit is better than implicit".

    ** - in Python 2.x, as the comment on the question states in Python 3 open replaces codecs.open

提交回复
热议问题