Difference between open and codecs.open in Python

前端 未结 8 533
一整个雨季
一整个雨季 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:07

    I was in a situation to open a .asm file and process the file.

    #https://docs.python.org/3/library/codecs.html#codecs.ignore_errors
    #https://docs.python.org/3/library/codecs.html#codecs.Codec.encode
    
    with codecs.open(file, encoding='cp1252', errors ='replace') as file:
    

    Without much trouble I am able to read the entire file, any suggestions?

提交回复
热议问题