日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

python报错UnicodeDecodeError: ‘gbk‘ codec can‘t decode解决方案

發布時間:2025/4/5 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python报错UnicodeDecodeError: ‘gbk‘ codec can‘t decode解决方案 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

問題:

解決方法:
讀取文件時加上encoding='utf-8'

代碼如下:

open(filename,encoding='utf-8')

正確程序測試:
word_count.py

def count_words(filename,target):"""計算文件filename中大致包含了多少個單詞:target"""try:with open(filename,'r',encoding='utf-8') as f_obj:lines = f_obj.readlines()text = ""except FileNotFoundError:msg = "Sorry,the file " + file_name1 + " does not exist."print(msg)else:for line in lines:text += linenum =text.lower().count(target)print("How many '"+ target +"' in "+ filename +"?\nThe answer is " + str(num) + ".")file_name = "the_spanish_influenza.txt" count_words(file_name,'the')

測試結果

參考:
[1]https://blog.csdn.net/lqzdreamer/article/details/76549256

總結

以上是生活随笔為你收集整理的python报错UnicodeDecodeError: ‘gbk‘ codec can‘t decode解决方案的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。