How to decode a unicode string Python - Stack Overflow
文章推薦指數: 80 %
You need to call encode function and not decode function, as item is already decoded. Like this: decoded_value = item.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 HowtodecodeaunicodestringPython[duplicate] AskQuestion Asked 6years,8monthsago Modified 2years,6monthsago Viewed 62ktimes 12 Thisquestionalreadyhasanswershere: Whydoesn'tthisconversiontoutf8work? (4answers) Closed6yearsago. Whatisthebestwaytodecodeanencodedstringthatlookslike:u'u\xf1somestring'? Background:Ihavealistthatcontainsrandomvalues(stringsandintegers),I'mtryingtoconverteveryiteminthelisttoastringthenprocesseachofthem. Turnsoutsomeoftheitemsareoftheformat:u'u\xf1somestring' WhenItriedconvertingtoastring,Igettheerror:UnicodeEncodeError:'ascii'codeccan'tencodecharacteru'\xf1'inposition1:ordinalnotinrange(128) Ihavetried item=u'u\xf1somestring' decoded_value=item.decode('utf-8','ignore') However,Ikeepgettingthesameerror. IhavereadupaboutunicodecharactersandtriedanumberofsuggestionsfromSObutnonehaveworkedsofar.AmImissingsomethinghere? stringpython-2.7unicodedecodeencode Share Improvethisquestion Follow editedMar22,2020at16:54 mfalade askedJan29,2016at11:26 mfalademfalade 1,11711goldbadge1616silverbadges1616bronzebadges 4 Ifit'saUnicodestring,it'salreadydecoded. – RemcoGerlich Jan29,2016at11:31 Youmayfindthisarticlehelpful:PragmaticUnicode,whichwaswrittenbySOveteranNedBatchelder. – PM2Ring Jan29,2016at11:32 Iassumeyou'reusingPython2.YoushouldalwaysmentionthePythonversionwithUnicodequestions(preferablywiththeappropriatetag)becausePython2&Python3handleUnicoderatherdifferently. – PM2Ring Jan29,2016at11:33 FWIW,s=u'u\xf1somestring'.encode('utf-8');prints,repr(s)printsuñsomestring'u\xc3\xb1somestring' – PM2Ring Jan29,2016at11:37 Addacomment | 2Answers 2 Sortedby: Resettodefault Highestscore(default) Trending(recentvotescountmore) Datemodified(newestfirst) Datecreated(oldestfirst) 15 Youneedtocallencodefunctionandnotdecodefunction,asitemisalreadydecoded. Likethis: decoded_value=item.encode('utf-8') Share Improvethisanswer Follow editedJan30,2016at7:29 answeredJan29,2016at11:30 SameerMirjiSameerMirji 2,0731515silverbadges2828bronzebadges 3 1 YoudecodetoUnicode,encodetobytestrings. – MarkTolonen Jan29,2016at17:01 @MarkTolonen:Sowhatpartofmyanswerdidyoufindwronghere?I'vespecificallyusedcodeblockstoindicateIwastakingaboutthemethodnameshere. – SameerMirji Jan30,2016at4:24 1 ThestringisalreadydecodedifitisaUnicodestring.item.encode('utf-8')makesanencoded_value.You(andtheOP)havetheterminologyconfused. – MarkTolonen Jan30,2016at7:27 Addacomment | 3 Thatstringalreadyisdecoded(it'saUnicodeobject).Youneedtoencodeitifyouwanttostoreitinafile(orsendittoadumbterminaletc.). Generally,whenworkingwithUnicode,youshould(inPython2)decodeallyourstringsearlyintheworkflow(whichyoualreadyseemtohavedone;manylibrariesthathandleinternettrafficwillalreadydothatforyou),thendoallyourworkonUnicodeobjects,andthenattheveryend,whenwritingthemback,encodethemtowhateverencodingyou'reusing. Share Improvethisanswer Follow answeredJan29,2016at11:31 TimPietzckerTimPietzcker 318k5656goldbadges494494silverbadges550550bronzebadges Addacomment | Nottheansweryou'relookingfor?Browseotherquestionstaggedstringpython-2.7unicodedecodeencodeoraskyourownquestion. TheOverflowBlog HowtoearnamillionreputationonStackOverflow:beofservicetoothers Therightwaytojobhop(Ep.495) FeaturedonMeta BookmarkshaveevolvedintoSaves Inboximprovements:markingnotificationsasread/unread,andafiltered... Revieweroverboard!Orarequesttoimprovetheonboardingguidancefornew... CollectivesUpdate:RecognizedMembers,Articles,andGitLab Shouldweburninatethe[script]tag? Linked 14 Whydoesn'tthisconversiontoutf8work? 0 Pythonprintsstringwith"+"signinbetween 0 CharacterDecodingencodedstringbacktooriginalstatepython Related 7319 WhatisthedifferencebetweenStringandstringinC#? 3288 HowdoIiterateoverthewordsofastring? 4567 HowdoIread/convertanInputStreamintoaStringinJava? 3469 Convertbytestoastring 2557 HowdoIgetasubstringofastringinPython? 4802 HowdoImakethefirstletterofastringuppercaseinJavaScript? 5276 HowdoIreplacealloccurrencesofastringinJavaScript? 7412 HowtocheckwhetherastringcontainsasubstringinJavaScript? 3588 DoesPythonhaveastring'contains'substringmethod? 2455 HowdoIlowercaseastringinPython? HotNetworkQuestions Theunusualphrasing"verb+the+comparativeadjective"intheLordoftheRingsnovels HowtogetridofUbuntuProadvertisementwhenupdatingapt? HowcanIkeepmyampfromtemperingthetoneofmyprocessor?(rockandhardmetalmusic) Howdouncomputablenumbersrelatetouncomputablefunctions? Myfavoriteanimalisa-singularandpluralform Wouldmerfolkgainanyrealadvantagefrommounts(andbeastsofburden)? Whydoesn'ttheMBRS1100SchottkydiodehaveanexponentialI/Vcharacteristic? ShouldIresendanapplication? WhatistheAmericanequivalentof"Icalledmymomtoaskafterher"? UnderstandingsuspiciousHTTPGETRequest WhatdothecolorsindicateonthisKC135tankerboom? Canyoufindit? Whatprotocolisthiswaveform? Realitycheck:PolarCO2lakescoexistingwithanequatorialH2Oocean Unsurewhatthesewatersoftenerdialsarefor Adecimal-basedunitoftime MakeaCourtTranscriber Whydoes«facture»mean"bill,invoice"? Movingframesmethod PreferenceofBJTtoMOSFET Howtoplug2.5mm²strandedwiresintoapushwirewago? Canaphotonturnaprotonintoaneutron? Whataretheargumentsforrevengeandretribution? Wouldextractinghydrogenfromthesunlessenitslifespan? morehotquestions lang-py Yourprivacy Byclicking“Acceptallcookies”,youagreeStackExchangecanstorecookiesonyourdeviceanddiscloseinformationinaccordancewithourCookiePolicy. Acceptallcookies Customizesettings
延伸文章資訊
- 1Day27 Python 基礎- 字符轉編碼操作 - iT 邦幫忙
上面那個看起來很像亂碼(不是亂馬),其實是unicode格式,証明我們已經確實的把 UTF-8 decode Unicode 編碼了,再來就是要把 Unicode encode gbk
- 2Unicode — pysheeet
In Python 3, strings are represented by Unicode instead of bytes. ... A byte string transfers to ...
- 3瞭解Unicode — Python Tutorial 0.1 說明文件
與外界溝通- decode與encode¶. 在上面我們學到了如何表示unicode 字串,但是事實上是, unicode 字串只能存在程式的內部,並沒有 ...
- 4Unicode & Character Encodings in Python: A Painless Guide
Encoding and Decoding in Python 3 ... Python 3's str type is meant to represent human-readable te...
- 5Converting Between Unicode and Plain Strings - O'Reilly
Unicode strings can be encoded in plain strings in a variety of ways, according to whichever enco...