Python報錯:SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xd6 in position 17: invalid continuation byte


1.示例代碼

#coding=utf-8
#
<div class="nam">中國</div>,用【正則】匹配出標簽里面的內容(“中國”),其中class的類名是不確定的 import re str = "<div class='nam'>中國</div>" res = re.findall(r'<div class=".*">(.*?)</div>',str) print(res) # 備注:res表示result

 

2.報錯截圖

 

 

3.報錯原因: 申明只是告訴python解釋器按這個編碼來解析,和文件本身編碼並沒有關系。新建python文件默認編碼是ANSI(GBK),而python文件內的申明是utf-8
 
4.解決方法
(1)將python文件轉成utf-8格式
(2)修改編碼申明為gbk,即:#-*-coding:GBK -*-

 

參考:https://blog.csdn.net/qq_32708193/article/details/89342880


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM