Python String encode() decode() - DigitalOcean
文章推薦指數: 80 %
This function returns the bytes object. If we don't provide encoding, “utf-8” encoding is used as default. Python Bytes decode(). Python bytes ...
Getalertedwhenassetsaredown,slow,orvulnerabletoSSLattacks—allfreeforamonth.Learnmore->We'rehiringBlogDocsGetSupportSalesProductsFeaturedProductsDropletsScalablevirtualmachinesKubernetesManagedKubernetesclustersAppPlatformGetappstomarketfasterFunctionsServerlesscomputesolutionDatabasesWorry-freesetup&maintenanceComputeDropletsKubernetesAppPlatformFunctionsStorageSpacesObjectStorageVolumesBlockStorageNetworkingVirtualPrivateCloud(VPC)CloudFirewallsLoadBalancersReservedIPsDNSManagedDatabasesMongoDBMySQLPostgreSQLRedis™ToolsandServicesAPICLISupportPlansTerraformProviderContainerManagementToolsMonitoringUptimeTeamsSeeallproductsSolutionsTryDigitalOceanforfreeClickheretosignupandget$200ofcredittotryourproductsover60days!SignupOurbusinesssolutionsWebsiteHostingVPSHostingByusecaseCloudVPNWeb&MobileAppsGameDevelopmentVideoStreamingSaaSPlatformsBlockchainQuestions?SpeakWithAnExpertTodaySeeallsolutionsMarketplaceCommunityOurcommunityCommunityHomeDevOpsanddevelopmentguidesCSS-TricksAllthingswebdesignResourcesTutorialsQuestionsAndAnswersToolsWriteforDOnationsCustomerStoriesDigitalOceanBlogGetInvolvedHatchStartupProgramOpenSourceSponsorshipsHacktoberfestDeployDOImpactDocumentationQuickStartDropletsStorageAppPlatformAPIReferenceDomainsandDNSPricingLoginSigninCommunityDigitalOceanSignupSignupCommunityDigitalOceanWe'rehiringBlogDocsGetSupportSalesTutorialsQuestionsTechTalksLearningPathsProductDocsSocialImpactSearchCommunityCONTENTSPythonStringencode()PythonBytesdecode()RelatedPythontimesleep()ViewVectorsinPython-AQuickIntroduction!View//Tutorial//PythonStringencode()decode()PublishedonAugust3,2022PythonPythonStringByPankajDeveloperandauthoratDigitalOcean.Whilewebelievethatthiscontentbenefitsourcommunity,wehavenotyetthoroughlyreviewedit.Ifyouhaveanysuggestionsforimprovements,pleaseletusknowbyclickingthe“reportanissue“buttonatthebottomofthetutorial.PythonStringencode()
Pythonstringencode()functionisusedtoencodethestringusingtheprovidedencoding.Thisfunctionreturnsthebytesobject.Ifwedon’tprovideencoding,“utf-8”encodingisusedasdefault.
PythonBytesdecode()
Pythonbytesdecode()functionisusedtoconvertbytestostringobject.Boththesefunctionsallowustospecifytheerrorhandlingschemetouseforencoding/decodingerrors.Thedefaultis‘strict’meaningthatencodingerrorsraiseaUnicodeEncodeError.Someotherpossiblevaluesare‘ignore’,‘replace’and‘xmlcharrefreplace’.Let’slookatasimpleexampleofpythonstringencode()decode()functions.
str_original='Hello'
bytes_encoded=str_original.encode(encoding='utf-8')
print(type(bytes_encoded))
str_decoded=bytes_encoded.decode()
print(type(str_decoded))
print('Encodedbytes=',bytes_encoded)
print('DecodedString=',str_decoded)
print('str_originalequalsstr_decoded=',str_original==str_decoded)
Output:
延伸文章資訊
- 1Decoding UTF-8 strings in Python - Stack Overflow
It's an encoding error - so if it's a unicode string, this ought to fix it: text.encode("windows-...
- 2Python3 bytes.decode()方法 - 菜鸟教程
Python3 bytes.decode()方法Python3 字符串描述decode() 方法以指定的编码格式解码bytes 对象。默认编码为'utf-8'。 语法decode()方法语法: ...
- 3Decode UTF-8 in Python | Delft Stack
- 4Remove Unicode Characters In Python
- 5Day27 Python 基礎- 字符轉編碼操作 - iT 邦幫忙
首先通過編碼 decode 轉換為 Unicode 編碼 · 然後通過解碼 encode 轉換為 UTF-8 編碼.