Python中的Unicode解碼錯誤(Unicode Decode Error in Python)
文章推薦指數: 80 %
問題描述Python中的Unicode解碼錯誤(Unicode Decode Error in Python) TDB = csv.reader(codecs.open('data/TDS.csv', 'rb', encoding='utf‑8'), delimiter=',', ... Python中的Unicode解碼錯誤(UnicodeDecodeErrorinPython)問題討論Python中的Unicode解碼錯誤(UnicodeDecodeErrorinPython)問題描述Python中的Unicode解碼錯誤(UnicodeDecodeErrorinPython)TDB = csv.reader(codecs.open('data/TDS.csv', 'rb', encoding='utf‑8'), delimiter=',', quotechar='"') ts = db.testCol for row in TDB: print row[1] T = {"t":row[1], "s": row[0]} post_id = ts.insert(T) I not sure why i can't encode it into utf‑8 while i want to insert data into database i must make it in utf8 format.UnicodeDecodeError: 'utf8' codec can't decode byte 0xf3 in position 36: invalid continuation byte Before i put the encoding function, i got this from pymongo. bson.errors.InvalidStringData: strings in documents must be valid UTF‑8 and i guess, this is the data it couldn't encode'compleja e intelectualmente retadora , el ladrÛn de orquÌdeas es uno de esos filmes que vale la pena ver precisamente por su originalidad . ' Anyone know how should i do? Thanks‑‑‑‑‑參考解法方法1:Ok, this might help..There are a list of encodings here:http://docs.python.org/2/library/codecs.html#standard‑encodingslatin‑1 is a common encoding used for languages in europe.The basic flow with dealing with encodings is:read in encoded content content.decode("source encoding") to unicodeencode from unicode to desired encoding, unicode_content.encode("desired encoding")You can try going through encodings that seem right and see which ones don't cause an error:enc = "latin‑1" f = open("TSD.csv", "r") content = f.read() # raw encoded content u_content = content.decode(enc) # decodes from enc to unicode utf8_content = u_content.encode("utf8") (by1myb、monkut)參考文件UnicodeDecodeErrorinPython(CCBY‑SA3.0/4.0)#Python#mongoDB#utf-8相關問題如何從控制台中導入的文件中訪問變量的內容?(HowdoIaccessthecontentsofavariablefromafileimportedinaconsole?)在python3.5的輸入列表中添加美元符號、逗號和大括號(Addingdollarsigns,commasandcurlybracketstoinputlistinpython3.5)為KeyError打印出奇怪的錯誤消息(StrangeerrormessageprintedoutforKeyError)django1.9中的fromdjango.views.generic.simpleimportdirect_to_template相當於什麼(Whatistheequivalentoffromdjango.views.generic.simpleimportdirect_to_templateindjango1.9)查詢嵌入列表中的數組(Queryingforarrayinembeddedlist)如何在Python中搜索子字符串是否在二進製文件中?(HowtosearchifasubstringisintoabinaryfileinPython?)為什麼要避免while循環?(Whyavoidwhileloops?)使用python的json模塊解析json請求(Parseajsonrequestusingjsonmoduleofpython)為什麼使用py2app模塊創建mac文件時出現錯誤?(WhyiamgettingErrorwhencreatingmacfileusingpy2appmodule?)當python線程在網絡調用(HTTPS)中並且發生上下文切換時會發生什麼?(Whathappenswhenthepythonthreadisinnetworkcall(HTTPS)andthecontextswitchhappens?)如何繪製一條帶斜率和一個點的線?Python(Howtoplotalinewithslopeandonepointgiven?Python)Pickle找不到我不使用的模塊?(Picklecan'tfindmodulethatIamnotusing?)留言討論提交送出編輯提交送出編輯提交送出回覆提交送出歡迎回家×使用Github帳號登入使用Google帳號登入取消免費加入CoderBridge×使用Github帳號註冊使用Google帳號註冊取消
延伸文章資訊
- 1Python中解码decode()与编码encode()与错误处理 ... - 博客园
errors may be given to set a different error handling scheme. The default for errors is 'strict' ...
- 2python解決漢字編碼問題:Unicode Decode Error | 程式前沿
python解決漢字編碼問題:Unicode Decode Error ... position ordinal not in range 128 UnicodeDecodeError: 'ut...
- 3Python decode()方法 - 菜鸟教程
Python decode() 方法以encoding 指定的编码格式解码字符串。默认编码为字符串编码。 语法. decode()方法语法: str.decode(encoding='UTF-8...
- 4codecs — Codec registry and base classes — Python 3.10.7 ...
This module defines base classes for standard Python codecs (encoders and ... The default error h...
- 5Python decode 非法字符
簡單紀錄一下docode. >>> a = "你好" >>> a.encode("utf-8").decode("utf-8", "ignore"). decode 的函數原型是decode([...