Python: UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)

后端 未结 4 814
名媛妹妹
名媛妹妹 2021-01-07 02:27

I\'m currently have an issue with my python 3 code.

 replace_line(\'Products.txt\', line, tenminus_str)

Is the line I\'m trying to turn in

相关标签:
4条回答
  • 2021-01-07 03:04

    Try adding encoding='utf8' if you are reading a file

    with open("../file_path", encoding='utf8'):
             # your code
    
    0 讨论(0)
  • 2021-01-07 03:16

    Change your function to:

    def replace_line(file_name, line_num, text):
        with open(file_name, 'r', encoding='utf8') as f:
            lines = f.readlines()
        lines[line_num] = text
        with open(file_name, 'w', encoding='utf8') as out:
            out.writelines(lines)
    

    encoding='utf8' will decode your UTF-8 file correctly.

    with automatically closes the file when its block is exited.

    Since your file started with \xef it likely has a UTF-8-encoding byte order mark (BOM) character at the beginning. The above code will maintain that on output, but if you don't want it use utf-8-sig for the input encoding. Then it will be automatically removed.

    0 讨论(0)
  • 2021-01-07 03:24

    Handling coding problems You can try adding the following settings to your head


    import sys
    reload(sys)
    sys.setdefaultencoding('utf-8')
    Type = sys.getfilesystemencoding()
    
    0 讨论(0)
  • 2021-01-07 03:28

    codecs module is just what you need. detail here

    import codecs
    def replace_line(file_name, line_num, text):
        f = codecs.open(file_name, 'r', encoding='utf-8')
        lines = f.readlines()
        lines[line_num] = text
        f.close()
        w = codecs.open(file_name, 'w', encoding='utf-8')
        w.writelines(lines)
        w.close()
    
    0 讨论(0)
提交回复
热议问题