2017-02-04 76 views
1

我目前有一个域名列表,其中一些是国际化的。试图解码国际化的网址

例如一个与此xn--nqv7f.com结束,但我希望它显示这样机构.com

我尝试它编码为asciiutf-8,但我似乎无法得到控制台或我的网站上打印出来喜欢这个。我使用python 3.5

'xn--nqv7f.com'.decode('utf-8') 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
AttributeError: 'str' object has no attribute 'decode' 

,当我尝试这个,我只得到这个

'xn--nqv7f.com'.encode("idna") 
b'xn--nqv7f.com' 

回答

1

只好编码然后解码

'xn--nqv7f.com'.encode("idna").decode('idna') 
'机构.com'