Python3: Decode UTF-8 bytes converted as string

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

If you want to encode and decode text, that's what the encode and decode methods are for: >>> a = "Gżegżółka" >>> b = a.encode('utf-8') ... 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 Python3:DecodeUTF-8bytesconvertedasstring AskQuestion Asked 4years,3monthsago Modified 4years,3monthsago Viewed 26ktimes 0 SupposeIhavesomethinglike: a="Gżegżółka" a=bytes(a,'utf-8') a=str(a) whichreturnsstringinform: b'G\xc5\xbceg\xc5\xbc\xc3\xb3\xc5\x82ka' Nowit'ssendassimplestring(Igetitasassertionfromevalfunction).HowtheheckcanInowgetnormalUTF-8formofstartingword?Ifthereissomebettercompressionthanstr(bytes(x))thenIwouldbegladtohear. pythonpython-3.xutf-8decodeencode Share Improvethisquestion Follow askedJul5,2018at22:56 Ch3shireCh3shire 99722goldbadges1010silverbadges3434bronzebadges 1 2 Idon'tknowwhatyoumeanby"Igetitasassertionfromevalfunction",butthatsoundslikeyou'redoingsomethingthat'saverybadideaincoderightoutsidethecodethatyoushowedus… – abarnert Jul5,2018at23:07 Addacomment  |  2Answers 2 Sortedby: Resettodefault Highestscore(default) Trending(recentvotescountmore) Datemodified(newestfirst) Datecreated(oldestfirst) 6 Ifyouwanttoencodeanddecodetext,that'swhattheencodeanddecodemethodsarefor: >>>a="Gżegżółka" >>>b=a.encode('utf-8') >>>b b'G\xc5\xbceg\xc5\xbc\xc3\xb3\xc5\x82ka' >>>c=b.decode('utf-8') >>>c 'Gżegżółka' Also,noticethatUTF-8isalreadythedefault,soyoucanjustdothis: >>>b=a.encode() >>>c=b.decode() Theonlyreasonyouneedtospecifyargumentsis: YouneedtousesomeotherencodinginsteadofUTF-8, Youneedtospecifyaspecificerrorhandler,like'surrogatereplace'insteadof'strict',or YourcodehastoruninPython3.0-3.1(whichalmostnobodyused). However,ifyoureallywantto,youcandowhatyouwerealreadydoing;youjustneedtoexplicitlyspecifytheencodinginthestrcall,justasyoudidinthebytescall: >>>a="Gżegżółka" >>>b=bytes(a,'utf-8') >>>b b'G\xc5\xbceg\xc5\xbc\xc3\xb3\xc5\x82ka' >>>c=str(b,'utf-8') >>>c Callingstronabytesobjectwithoutanencoding,asyouweredoing,doesn'tdecodeit,anddoesn'traiseanexceptionlikecallingbytesonastrwithoutanencoding,becausethemainjobofstristogiveyouastringrepresentationoftheobject—andthebeststringrepresentationofabytesobjectisthatb'…'. Share Improvethisanswer Follow editedJul5,2018at23:06 answeredJul5,2018at23:00 abarnertabarnert 341k4444goldbadges573573silverbadges652652bronzebadges Addacomment  |  0 Ifoundit.Thesimplestwaytoconvertstringrepresentationofbytestobytesagainisthroughtheevalstatement: a="Gżegżółka" a=bytes(a,'utf-8') a=str(a)#thisistheinputwedealwith a=eval(a)#that'showwetransformaintobytes a=str(a,'utf-8')#...andnowweconvertitintostring print(a) Share Improvethisanswer Follow answeredJul6,2018at6:41 Ch3shireCh3shire 99722goldbadges1010silverbadges3434bronzebadges 3 As@abarnertcommenteditlookslikeyouaretryingtofixthewrongcodebutifyouaregoingtodothisatleastuseast.literal_evalinstaedofeval. – StopharmingMonica Jul6,2018at7:13 Welp,nowIfoundthatIjustloadscriptwithoututf-8encoding.Butstill,Ithinkthattryingtodecipherstr(bytes(x))isinterestingproblem:) – Ch3shire Jul6,2018at7:30 1 Idon'tknowwhatyouaretalkingabout,thereisnothinglikethatinyourquestion.AsyoufoundoutyoucanreconstructabytesobjectfromitsstringrepresentationbutyoucannotdothatwitharbitraryobjectssoIwouldsayamoreinterestingproblemishowyoumanagedtopaintyourselfintothatcornerandhowtoavoidit. – StopharmingMonica Jul6,2018at8:13 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?Browseotherquestionstaggedpythonpython-3.xutf-8decodeencodeoraskyourownquestion. TheOverflowBlog HowtoearnamillionreputationonStackOverflow:beofservicetoothers Therightwaytojobhop(Ep.495) FeaturedonMeta BookmarkshaveevolvedintoSaves Inboximprovements:markingnotificationsasread/unread,andafiltered... Revieweroverboard!Orarequesttoimprovetheonboardingguidancefornew... CollectivesUpdate:RecognizedMembers,Articles,andGitLab Shouldweburninatethe[script]tag? Linked -1 Howtoconvertthestringrepresentationofabinarystringfromatextfilebackintotheutf8encodedtextitcamefrom? 0 HowcanIexportatablefromawebpagetocsv? Related 1323 UTF-8allthewaythrough 2647 HowdoIparseastringtoafloatorint? 2851 Convertstring"Jun120051:33PM"intodatetime 3469 Convertbytestoastring 2557 HowdoIgetasubstringofastringinPython? 974 What'sthedifferencebetweenUTF-8andUTF-8withBOM? 3588 DoesPythonhaveastring'contains'substringmethod? 2455 HowdoIlowercaseastringinPython? 1347 BestwaytoconvertstringtobytesinPython3? 881 UnicodeDecodeError:'charmap'codeccan'tdecodebyteXinpositionY:charactermapsto HotNetworkQuestions Areyougettingtiredofregularcrosswords? Shouldselectedoptionsberemovedfromsingle-andmulti-selectdropdownlists? Vivadoconstraintswizardsuggestsalotofnonsensegeneratedclocks HowdoGPSreceiverscommunicatewithsatellites? Howcanmyaliensymbiotesidentifyeachother? Single-rowSettingstable:prosandconsofJoinsvsscalarsubqueries Howtoimportanchor-langwiththeinit-if-neededcargofeatureenabled FMEmergefeatures(many-to-one)withpriority Howdoyoucalculatethetimeuntilthesteady-stateofadrug? HowtoruntheGUIofWindowsFeaturesOn/OffusingPowershell HowIcanremoveautoincrementfromaPrimarykeyinpostgresql? DidMS-DOSeverdropabilitytosupportnon-IBMPCcompatiblemachines? WhydidGodprohibitwearingofgarmentsofdifferentmaterialsinLeviticus19:19? Howdoparty-listsystemsaccommodateindependentcandidates? Wouldextractinghydrogenfromthesunlessenitslifespan? WhydoNorthandSouthAmericancountriesoffercitizenshipbasedonunrestrictedJusSoli(rightofsoil)? Findanddeletepartiallyduplicatelines Whydostringhashcodeschangeforeachexecutionin.NET? Howtoproperlycolorcellsinalatextablewithoutscrewingupthelines? HowdoIresolverecentearthworksaroundmyfuturefenceline? Realitycheck:PolarCO2lakescoexistingwithanequatorialH2Oocean Isthe2...g6DutchautomaticallywinningforWhite? Whatprotocolisthiswaveform? Whyarefighterjetssoloudwhendoingslowflight? morehotquestions Questionfeed SubscribetoRSS Questionfeed TosubscribetothisRSSfeed,copyandpastethisURLintoyourRSSreader. lang-py Yourprivacy Byclicking“Acceptallcookies”,youagreeStackExchangecanstorecookiesonyourdeviceanddiscloseinformationinaccordancewithourCookiePolicy. Acceptallcookies Customizesettings  



請為這篇文章評分?