python将PunyCode转换回Unicode

发布于 2025-01-22 07:06:55 字数 649 浏览 4 评论 0 原文

我正在尝试将联系人添加到 sendgrid 中,该数据偶尔将用户电子邮件存储在 punycode

无论如何,如果我尝试添加 ascii 版本,则有一个错误,因为 sendgrid 不接受它 - 但是它确实接受了Unicode版本。

因此,有没有办法将它们转换为Python。

因此,我认为长话短说,有没有办法将 punycode 解码为unicode?

编辑

正如我尝试的评论所建议的 'example-email@yahóo.com'.encode('punyCode')。decode()返回 [email  nbsp; procectived] ,这是不当解决方案。

提前致谢。

I'm trying to add contacts to Sendgrid from a db which occasionally is storing the user email in punycode [email protected] which translates to example-email@yahóo.com in Unicode.

Anyway if I try and add the ascii version there's an error because sendgrid doesn't accept it - however it does accept the Unicode version.

So is there a way to convert them in python.

So I think long story short is there a way to decode punycode to Unicode?

Edit

As suggested in comments i tried
'example-email@yahóo.com'.encode('punycode').decode() which returns [email protected] so this is incorrect outside of python so is not a valid solution.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏日落 2025-01-29 07:06:55

xn - 在您编码的电子邮件地址中:

IDNA的ACE前缀为“ Xn-”或任何资本化。

因此,应用 IDNA 编码(请参阅 python> python特定的编码):

codec idna 实施 rfc 3490 ,另请参见< a href =“ https://docs.python.org/3.10/library/codecs.html#module-encodings.idna” rel =“ noreferrer”> eccodings.idna
仅支持errors ='light'。

结果

'yahóo.com'.encode('idna').decode()
# 'xn--yaho-sqa.com'

反之:

'xn--yaho-sqa.com'.encode().decode('idna')
# 'yahóo.com'

您可以使用 idna 而不是:

支持应用程序中国际化域名(IDNA)

此库还提供了对Unicode技术标准46的支持
unicode IDNA兼容性处理

这可以作为“ “ noreferrer”> encodings.idna ” python标准库的模块,但仅
支持较旧的取代IDNA规范( rfc 3490 )。

There is the xn-- ACE prefix in your encoded e-mail address:

The ACE prefix for IDNA is "xn--" or any capitalization thereof.

So apply the idna encoding (see Python Specific Encodings):

codec idna Implement RFC 3490, see also encodings.idna.
Only errors='strict' is supported.

Result:

'yahóo.com'.encode('idna').decode()
# 'xn--yaho-sqa.com'

and vice versa:

'xn--yaho-sqa.com'.encode().decode('idna')
# 'yahóo.com'

You could use the idna library instead:

Support for the Internationalised Domain Names in Applications (IDNA)
protocol as specified in RFC 5891. This is the latest version of
the protocol and is sometimes referred to as “IDNA 2008”.

This library also provides support for Unicode Technical Standard 46,
Unicode IDNA Compatibility Processing.

This acts as a suitable replacement for the “encodings.idna” module that comes with the Python standard library, but which only
supports the older superseded IDNA specification (RFC 3490).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文