将html + hex电子邮件地址转换为可读字符串Python 3

问题描述 投票:0回答:1

我一直试图找到一个在线转换器,或Python3函数,用于转换html + hex格式的电子邮件地址,例如:%69%6efo ---> info

%69 : i
%6e : n
&#64 : @
(source: http://www.asciitable.com/)

...等等..

以下所有网站均未转换“单词”中合并的十六进制和html代码:

https://www.motobit.com/util/charset-codepage-conversion.asp
https://www.binaryhexconverter.com/ascii-text-to-binary-converter
https://www.dcode.fr/ascii-code
http://www.unit-conversion.info/texttools/ascii/
https://mothereff.in/binary-ascii

我很感激任何建议。 TXS。

python-3.x hex ascii converters email-address
1个回答
1
投票

尝试使用html.unescape()HTMLParser#unescape,具体取决于您使用的是哪个版本的Python:https://stackoverflow.com/a/2087433/2675670

由于这是十六进制值和常规字符的混合,我认为我们必须提出一个自定义解决方案:

word = "%69%6efo"

while word.find("%") >= 0:
    index = word.find("%")
    ascii_value = word[index+1:index+3]
    hex_value = int(ascii_value, 16)
    letter = chr(hex_value)
    word = word.replace(word[index:index+3], letter)

print(word)

也许有更简化的“Pythonic”方式,但它适用于测试输入。

© www.soinside.com 2019 - 2024. All rights reserved.