Discuss / Python / 读写中文报错

读写中文报错

Topic source

UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)

王新美男

#2 Created at ... [Delete] [Delete and Lock User]

看看你的py文件最上面是否是

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

txt 文件里写了“你好,再见”,想用python读出来结果报错 (result, consumed) = self._buffer_decode(data, self.errors, final) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb2 in position 0: invalid start byte

file=open('abc.txt','r',encoding='utf-8')
file.read()

廖雪峰

#4 Created at ... [Delete] [Delete and Lock User]

你的文件不是utf-8编码


  • 1

Reply