Python load json file with UTF-8 BOM header - Stack Overflow

文章推薦指數: 80 %
投票人數:10人

You can open with codecs : import json import codecs json.load(codecs.open('sample.json', 'r', 'utf-8-sig')). or decode with utf-8-sig ... Home Public Questions Tags Users Companies Collectives ExploreCollectives Teams StackOverflowforTeams –Startcollaboratingandsharingorganizationalknowledge. CreateafreeTeam WhyTeams? Teams CreatefreeTeam Collectives™onStackOverflow Findcentralized,trustedcontentandcollaboratearoundthetechnologiesyouusemost. LearnmoreaboutCollectives Teams Q&Aforwork Connectandshareknowledgewithinasinglelocationthatisstructuredandeasytosearch. LearnmoreaboutTeams PythonloadjsonfilewithUTF-8BOMheader AskQuestion Asked 9years,11monthsago Modified 1year,6monthsago Viewed 74ktimes 53 Ineededtoparsefilesgeneratedbyothertool,whichunconditionallyoutputsjsonfilewithUTF-8BOMheader(EFBBBF).Isoonfoundthatthiswastheproblem,asPython2.7modulecan'tseemtoparseit: >>>importjson >>>data=json.load(open('sample.json')) ValueError:NoJSONobjectcouldbedecoded RemovingBOM,solvesit,butIwonderifthereisanotherwayofparsingjsonfilewithBOMheader? pythonjson Share Improvethisquestion Follow askedOct31,2012at11:01 thetatheta 23.4k3636goldbadges114114silverbadges157157bronzebadges 1 2 Python:HowtofixUnexpectedUTF-8BOMerrorwhenusingjson.loads – GrijeshChauhan Nov27,2019at9:43 Addacomment  |  7Answers 7 Sortedby: Resettodefault Highestscore(default) Trending(recentvotescountmore) Datemodified(newestfirst) Datecreated(oldestfirst) 92 Youcanopenwithcodecs: importjson importcodecs json.load(codecs.open('sample.json','r','utf-8-sig')) ordecodewithutf-8-sigyourselfandpasstoloads: json.loads(open('sample.json').read().decode('utf-8-sig')) Share Improvethisanswer Follow editedOct31,2012at11:25 answeredOct31,2012at11:20 PavelAnossovPavelAnossov 58.8k1414goldbadges146146silverbadges123123bronzebadges 2 21 Istronglyrecommendusingio.open()overcodecs.open():json.load(io.open('sample.json','r',encoding='utf-8-sig')).Theiomoduleismorerobustandfaster. – MartijnPieters ♦ Mar2,2017at9:53 @MartijnPieters:Thanksforthatcomment,goodtoknow.Ifoundthisdiscussionofthedifferencesthatmightbeuseful:groups.google.com/forum/#!topic/comp.lang.python/s_eIyt3KoLE – Bdoserror Apr4,2017at18:02 Addacomment  |  33 Simple!Youdon'tevenneedtoimportcodecs. withopen('sample.json',encoding='utf-8-sig')asf: data=json.load(f) Share Improvethisanswer Follow editedApr10,2021at7:21 JohnRPerry 3,69322goldbadges3636silverbadges5959bronzebadges answeredJun6,2019at22:38 aerinaerin 18.3k2828goldbadges9393silverbadges130130bronzebadges Addacomment  |  5 Sincejson.load(stream)usesjson.loads(stream.read())underthehood,itwon'tbethatbadtowriteasmallheplerfunctionthatlstripstheBOM: fromcodecsimportBOM_UTF8 deflstrip_bom(str_,bom=BOM_UTF8): ifstr_.startswith(bom): returnstr_[len(bom):] else: returnstr_ json.loads(lstrip_bom(open('sample.json').read())) Inothersituationswhereyouneedtowrapastreamandfixitsomehowyoumaylookatinheritingfromcodecs.StreamReader. Share Improvethisanswer Follow answeredOct31,2012at11:21 newtovernewtover 30.3k1111goldbadges8080silverbadges8888bronzebadges 4 Whynotusethestringstripfunction? – SamStoelinga Oct21,2013at14:20 3 @SamStoelinga,sincestripfunctionreceivesnotaprefixbutasetofcharacterstoremove.Thatityouneedtoeitherdecodethebyte-stringintotheunicodeorusetheapproachabovetobesureyouleft-striponlytheUTF-8BOM. – newtover Oct21,2013at18:31 I'mgettinganerrorthatsaysTypeError:expectedstr,bytesoros.Pathlikeobject,not_io.TextIOWrapper – Zypps987 Jun4,2017at16:14 @Zypps987,thesnippetassumespython2whereread()returnsbytes.Tomakethesnippetworkinpython3youwillneedtoencodeBOM_UTF8to'utf-8'.Butyoudon'tneedthis,whenyouhaveutf-8-sigencoding. – newtover Jun4,2017at19:44 Addacomment  |  4 youcanalsodoitwithkeywordwith importcodecs withcodecs.open('samples.json','r','utf-8-sig')asjson_file: data=json.load(json_file) orbetter: importio withio.open('samples.json','r',encoding='utf-8-sig')asjson_file: data=json.load(json_file) Share Improvethisanswer Follow editedMay4,2019at6:47 RayHulha 10.1k55goldbadges5151silverbadges5050bronzebadges answeredMar30,2019at15:24 MohamedAliMimouniMohamedAliMimouni 10144bronzebadges Addacomment  |  0 Ifthisisaone-off,averysimplesuperhigh-techsolutionthatworkedforme... OpentheJSONfileinyourfavoritetexteditor. Select-all Createanewfile Paste Save. BOOM,BOMheadergone! Share Improvethisanswer Follow answeredDec4,2017at8:51 MikeNMikeN 5,85533goldbadges2323silverbadges2121bronzebadges 0 Addacomment  |  0 IremovedtheBOMmanuallywithLinuxcommand. FirstIcheckifthereareefbbbfbytesforthefile,withheadi_have_BOM|xxd. ThenIrunddbs=1skip=3if=i_have_BOM.jsonof=I_dont_have_BOM.json. bs=1process1byteeachtime,skip=3,skipthefirst3bytes. Share Improvethisanswer Follow answeredMar24,2020at1:01 RickRick 6,28022goldbadges3939silverbadges7070bronzebadges Addacomment  |  0 I'musingutf-8-sigjustwithimportjson withopen('estados.json',encoding='utf-8-sig')asjson_file: data=json.load(json_file) print(data) Share Improvethisanswer Follow answeredJun10,2020at11:20 RodrigoGrossiRodrigoGrossi 1511bronzebadge Addacomment  |  YourAnswer ThanksforcontributingananswertoStackOverflow!Pleasebesuretoanswerthequestion.Providedetailsandshareyourresearch!Butavoid…Askingforhelp,clarification,orrespondingtootheranswers.Makingstatementsbasedonopinion;backthemupwithreferencesorpersonalexperience.Tolearnmore,seeourtipsonwritinggreatanswers. Draftsaved Draftdiscarded Signuporlogin SignupusingGoogle SignupusingFacebook SignupusingEmailandPassword Submit Postasaguest Name Email Required,butnevershown PostYourAnswer Discard Byclicking“PostYourAnswer”,youagreetoourtermsofservice,privacypolicyandcookiepolicy Nottheansweryou'relookingfor?Browseotherquestionstaggedpythonjsonoraskyourownquestion. TheOverflowBlog HowtoearnamillionreputationonStackOverflow:beofservicetoothers Therightwaytojobhop(Ep.495) FeaturedonMeta BookmarkshaveevolvedintoSaves Inboximprovements:markingnotificationsasread/unread,andafiltered... Revieweroverboard!Orarequesttoimprovetheonboardingguidancefornew... CollectivesUpdate:RecognizedMembers,Articles,andGitLab Shouldweburninatethe[script]tag? Linked 0 Pythoncan'tparsemylistofints 1 Requests-PythonParsingJSONerror-loadwithcodecs 1 Pythonsplit()notworkingasexpectedforfirstlineinfile 0 PythonLoadUTF-8JSON 1 WhydoIgetaValueErrorwithJsonusingawindowstxtfileencodedwithutf-8? 0 PythonJSONLoadingfileerrors 0 Readingaresponsefromrequest.get 1 Loopactiononlistdoesn'tperformcorrectlyonjustthefirstitem 1 Unabletoreadajsonfileinpython 0 ImportingjsonfilesonGUIusingpython.gettingJSONDecodeError Seemorelinkedquestions Related 1514 UsedifferentPythonversionwithvirtualenv 1182 ParsingJSONwithUnixtools 2573 HowtoupgradeallPythonpackageswithpip? 3063 HowdoIdeleteafileorfolderinPython? 3573 HowdoIPOSTJSONdatawithcURL? 981 HowtoPOSTJSONdatawithPythonRequests? 1557 HowdoIwriteJSONdatatoafile? 1714 HowtoprettyprintaJSONfile? HotNetworkQuestions Movingframesmethod InD&D3.5,whathappenswhenyouplopaheadbandofintellectonananimal? Howcanmyaliensymbiotesidentifyeachother? InD&D3.5,canafamiliarbetemporarilydismissed? CPLEXstuckinsolvemethod-dualsimplexsolvedmodel What'sthedifferencebetween'Dynamic','Random',and'Procedural'generations? IfthedrowshadowbladeusesShadowSwordasarangedattack,doesitthrowasword(thatitthenhastoretrievebeforeusingitagain)? Canananimalfilealawsuitonitsownbehalf? Findanddeletepartiallyduplicatelines AmIreallyrequiredtosetupanInheritedIRA? sshhowtoallowaverylimiteduserwithnohometologinwithpubkey Canaphotonturnaprotonintoaneutron? LeavingaTTjobthenre-enteringacademia:Areaofbusinessandmanagement WhatdothecolorsindicateonthisKC135tankerboom? Single-rowSettingstable:prosandconsofJoinsvsscalarsubqueries Howdoyoucalculatethetimeuntilthesteady-stateofadrug? DidMS-DOSeverdropabilitytosupportnon-IBMPCcompatiblemachines? Doesindecentexposurerequireintentionality? Howtoelegantlyimplementthisoneusefulobject-orientedfeatureinMathematica? Whenisthefirstelementintheargumentlistregardedasafunctionsymbolandwhennot? HowtoviewpauseandviewcurrentsolutioninCPLEXOptimisationStudio? HowIcanremoveautoincrementfromaPrimarykeyinpostgresql? CounterexampleforChvatal'sconjectureinaninfiniteset HowtoruntheGUIofWindowsFeaturesOn/OffusingPowershell morehotquestions Questionfeed SubscribetoRSS Questionfeed TosubscribetothisRSSfeed,copyandpastethisURLintoyourRSSreader. lang-py Yourprivacy Byclicking“Acceptallcookies”,youagreeStackExchangecanstorecookiesonyourdeviceanddiscloseinformationinaccordancewithourCookiePolicy. Acceptallcookies Customizesettings  



請為這篇文章評分?