简体   繁体   English

将字符串解码为 UTF-8(URL/百分比编码字符串)

[英]Decoding string to UTF-8 (URL/Percent-encoding strings)

In my Python 3 program I've come across weirdly encoded strings such as "abol%C3%A2t" (abolât) and "%C5%93ufs" (œufs).在我的 Python 3 程序中,我遇到了一些奇怪的编码字符串,例如“abol%C3%A2t”(abolât)和“%C5%93ufs”(œufs)。 I get them from WiktionaryParser.我从 WiktionaryParser 得到它们。

I've only found ways to encode it to UTF-8 for quoted-printable strings, which is not the case here.我只找到了将其编码为 UTF-8 以用于带引号的可打印字符串的方法,但此处并非如此。 How can I change a string from "%C3%A9vitables" to "évitables"?如何将字符串从“%C3%A9vitables”更改为“évitables”?

These strings are Percent-encoded .这些字符串是百分比编码的 Use the urllib.parse module to decode them:使用urllib.parse模块解码它们:

import urllib.parse

s = "%C5%93ufs"
s = urllib.parse.unquote(s)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM