Python3: Decode UTF-8 bytes converted as string
文章推薦指數: 80 %
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
延伸文章資訊
- 1Python3 bytes.decode()方法 - 菜鸟教程
Python3 bytes.decode()方法Python3 字符串描述decode() 方法以指定的编码格式解码bytes 对象。默认编码为'utf-8'。 语法decode()方法语法: ...
- 2Python 3 Bytes.decode 遇到的问题— qytz-notes v0.1
使用Python 3 的 Subprocess 模块执行shell 命令,读取到的结果的类型是 bytes ,如果是文本需要转成 str 类型。 一般来说,Linux 的中文环境大都使用utf-...
- 3python3大作戰之encode與decode講解- 程式人生
因為開頭跟大家講過,python3預設就是unicode,utf-8可以看成是unicode的一個擴充套件集,所以其實name本身是一個unicode編碼的,所以可以通過encode ...
- 4Python3 decode()方法_w3cschool - 编程狮
Python3 decode()方法Python3 字符串描述decode() 方法以指定的编码格式解码 ... 语法decode()方法语法: str.decode(encoding=UTF-...
- 5python3 decode utf-8在PTT/Dcard完整相關資訊| 萌寵公園-2022年8月
關於「python3 decode utf-8」標籤,搜尋引擎有相關的訊息討論:. Unicode HOWTO — Python 3.9.4 documentationThe default e...