解决python代码中出现SyntaxError: Non-UTF-8 code starting with \xb8错误

解决python代码中出现SyntaxError: Non-UTF-8 code starting with '\xb8’错误

问题描述:

当程序中出现如下的错误时

File "hw1.py", line 1
SyntaxError: Non-UTF-8 code starting with '\xb5' in file hw1.py on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

是由于编码问题导致的

解决方案:

在源码的首部添加如下内容,保存源码即可

#coding=gbk
import io
import sys
import urllib.request
sys.stdout=io.TextIOWrapper(sys.stdout.buffer,encoding='utf8')
res=urllib.request.urlopen('http://www.baidu.com')
htmlBytes=res.read()
print(htmlBytes.decode('utf-8'))

该解决方案转载于
https://blog.csdn.net/weixin_40205234/article/details/86487798

你可能感兴趣的:(解决python代码中出现SyntaxError: Non-UTF-8 code starting with \xb8错误)