How to Fix json.loads Unexpected UTF-8 BOM Error in Python
文章推薦指數: 80 %
Solution 1 Decode content using utf-8-sig. In this solution, we can use decode() method on the return value of the string.encode() method. This ... Contact AboutUs WriteforUs Subscribe HowtoFixjson.loadsUnexpectedUTF-8BOMErrorinPython bySpeedySenseEditorial|LastUpdated:October26,2021 InPython,Youwillgetanerrorwhileretrievingthedatafromany3rdpartyAPIrequest.Infact,whenresponsecontentconvertstoJSONformatusingjson.loadsmethod,itthrowsanjson.decoder.JSONDecodeError:UnexpectedUTF-8BOMerror.Inthisarticlewearegoingtoseehowtofixjson.loads()UnexpectedUTF-8BOMerrorinPython. HowtoFixjson.loadsUnexpectedUTF-8BOMerrorinPython.WehaveseensolutionstofixUnexpectedUTF-8BOMerrorswhenusingjson.loadsinPython. Theerrorwasoccurbythejson.loads(r.text),sowhentextcontentconverttoJSONformatwehavegettingfollowingerror: json.decoder.JSONDecodeError:UnexpectedUTF-8BOM(decodeusingutf-8-sig):line1column1(char0) TheresponsecontentcomingfromtheAPI,but\ufeffUnicodecharactercomingatthebeginning.ThisUnicodecharactervalue\ufeff(or\xef\xbb\xbfinbinary)isabyteordermark(BOM)character. Python:Fixjson.loadsUnexpectedjson.decoderUTF-8BOMError Followingare4differentsolutions.Basicallyallfollowingsolutionswehavetodecodethedatausingutf-8-signencoding.Thiswaywecanfixtheerror. Solution1Decodecontentusingutf-8-sig Inthissolution,wecanusedecode()methodonthereturnvalueofthestring.encode()method.Thisisthemostefficientsolutiontofixthiserror. decoded_data=r.text.encode().decode('utf-8-sig') data=json.loads(decoded_data) Solution2Decoderesponsecontent Thissolutionisastraightforwardmethodtofixthisissue.Wehaveusedthedecode()methodonr.content. decoded_data=r.content.decode('utf-8-sig') data=json.loads(decoded_data) Solution3Encoderequests.responseobject Inthissolution,youcanusetheencodingpropertyontheresponseobject.Thiswaywecanskipthepreviousexamplesshowingthecallingofencode()anddecode()methods. r.encoding='utf-8-sig' data=json.loads(r.text) Solution4UsePythoncodecsmodule YoucanusethePythoncodecsmodule.Wecanusethecodecs.decode()methodtodecodethedatausingutf-8-sigencoding.Thecodecs.decode()methodacceptsabytesobject.Thus,youhavetoconvertthestringintoabytesobjectusingencode()method. decoded_data=codecs.decode(r.text.encode(),'utf-8-sig') data=json.loads(decoded_data) BottomLine Allinall,ifthejson.loads()methodthrowsanunexpectedUTF-8BOMerror.ItmeansBOMvaluesareexistingintheresponsedata.Inthisarticlewehaveseen4differentsolutionstoridoutthisjson.loadsUnexpectedUTF-8BOMerrorinPython. Anothermostcommonerror:objectarrayscannotbeloadedwhenallow_pickle=false.Youcancheck fixexception tofindanappropriatesolution. Wehopeyouhavefoundthisarticlehelpful.Letusknowyourquestionsorfeedbackifanythroughthecommentsectioninbelow.Youcansubscribeournewsletterandgetnotifiedwhenwepublishnewarticles.Moreover,youcanexplorehereotherinterestingarticles. ShareTweetSharePinShare1SharesIfyoulikeourarticle,pleaseconsiderbuyingacoffeeforus.Thanksforyoursupport!Buymeacoffee!TagsJSONPythonRequests ShareTweetSharePinShare1Shares Postnavigation PreviousArticleWhatHappensWhenYouInstallTooManyShopifyAppsNextArticle6BestAndroidAppsToImproveYourVocabulary JointheDiscussion.Cancel Savemyname,email,andwebsiteinthisbrowserforthenexttimeIcomment. Δ Search LatestPosts WhatistheDarkWeb?HowtoPreventYourDatafromBeingStolen?August30,2022 11BestAngularJSFrameworksforYourNextWebAppDevelopmentAugust10,2022 ShopifyMultivendorMarketplaceMay30,2022 HowtoEmbedaVideoinWordPressBlogPosts?[TheUltimateGuide]April3,2022 CategoriesCategoriesSelectCategoryCloudComputingDatabaseE-CommerceExamFreebiesGeneralHTML/CSSJavaScriptjQueryMobileAppsMobileWebsitesNetworkingOdooOnlineToolsPHPProgrammingPythonSEOShopifyStandardSwiftUbuntuUncategorizedWordpress Subscribe Enteryouremailaddresstosubscribetothisblogandreceivenotificationsofnewpostsbyemail.
延伸文章資訊
- 1Python load json file with UTF-8 BOM header - Stack Overflow
You can open with codecs : import json import codecs json.load(codecs.open('sample.json', 'r', 'u...
- 2json.decoder.JSONDecodeError: Unexpected UTF-8 BOM ...
Python3解析json文件时报错:json.decoder.JSONDecodeError: Unexpected UTF-8 BOM (decode using utf-8-sig): l...
- 3How to remove BOM from any text/XML file - IBM
- 4line 1 column 1 (char 0) ---While Tuning gpt2.finetune ...
問題描述JSONDecodeError: Unexpected UTF‑8 BOM (decode using utf‑8‑sig): line 1 column 1 (char 0) ‑‑‑W...
- 5What is the difference between utf-8 and utf-8-sig? - Stack Overflow