Converting domain names to idn in python

前端 未结 2 1398
别那么骄傲
别那么骄傲 2021-01-02 09:33

I have a long list of domain names which I need to generate some reports on. The list contains some IDN domains, and although I know how to convert them in python on the com

2条回答
  •  太阳男子
    2021-01-02 10:14

    you need to know in which encoding you file was saved. This would be something like 'utf-8' (which is NOT Unicode) or 'iso-8859-1' or 'cp1252' or alike.

    Then you can do (assuming 'utf-8'):

    
    infile = open(sys.argv[1])
    
    for line in infile:
        print line,
        domain = line.strip().decode('utf-8')
        print type(domain)
        print "IDN:", domain.encode("idna")
        print
    

    Convert encoded strings to unicode with decode. Convert unicode to string with encode. If you try to encode something which is already encoded, python tries to decode first, with the default codec 'ascii' which fails for non-ASCII-values.

提交回复
热议问题