Punycode Converter Library for Python
You can convert Punycode domain to/from Unicode domain with only one function: punycode.convert().
How to use
Install
pip install punycode
Function
There is only one function punycode.convert().
- input:
input_text: string
Input can be both a Punycode domain and a Unicode domain.
- return:
output_text: string
If you input Punycode domain, the return will be a Unicode domian.
On the other hand, if you input a Unicode domain, you will get a Punycode domain.
Code sample
import punycode
# Convert Unicode domain to Punycode domain
str1 = "美しい.世界"
str2 = "こっち.みんな"
str3 = "日本語.jp"
print(f"{str1} -> {punycode.convert(str1)}")
print(f"{str2} -> {punycode.convert(str2)}")
print(f"{str3} -> {punycode.convert(str3)}")
# 僕だけの.世界 -> xn--08j3a5b142t.xn--rhqv96g
# こっち.みんな -> xn--l8j9flb8a.xn--q9jyb4c
# 日本語.jp -> xn--wgv71a119e.jp
# Punycode domain to Unicode domain
str4 = "xn--n8jub8754b.xn--rhqv96g"
str5 = "xn--28j2af.xn--q9jyb4c"
str6 = "xn--wgv71a119e.jp"
print(f"{str4} -> {punycode.convert(str4)}")
print(f"{str5} -> {punycode.convert(str5)}")
print(f"{str6} -> {punycode.convert(str6)}")
# xn--n8jub8754b.xn--rhqv96g -> 美しい.世界
# xn--28j2af.xn--q9jyb4c -> こっち.みんな
# xn--wgv71a119e.jp -> 日本語.jp
ascii_only option (v0.2.0 or later)
If you don't want to convert a Punycode domain to Unicode when you use convert() function, you can use ascii_only option. (Default: False)
With this option True, convert() will convert only Unicode domains.
# ascii_only option (v0.2.0 or later)
ascii_only = True
# Only Unicode domains will be converted if ascii_only=True
print(f"{str4} -> {punycode.convert(str4, ascii_only)}")
print(f"{str5} -> {punycode.convert(str5, ascii_only)}")
print(f"{str6} -> {punycode.convert(str6, ascii_only)}")
# xn--n8jub8754b.xn--rhqv96g -> xn--n8jub8754b.xn--rhqv96g
# xn--28j2af.xn--q9jyb4c -> xn--28j2af.xn--q9jyb4c
# xn--wgv71a119e.jp -> xn--wgv71a119e.jp
This option is available on 0.2.0 or later version, so if you use v0.1.0, please update punycode.
pip install -U punycode
Metadata
Release files for punycode 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| punycode-0.2.1.tar.gz | 3.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| punycode-0.2.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 8.1 kB
Release files / punycode-0.2.1.tar.gz
| Download URL | punycode-0.2.1.tar.gz |
|---|---|
| Size | 3.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2619d4cc3d517f5b15f092d2e4108f37a44689514207f359ae6c5edf4fcc341d
|
|
BLAKE2b-256 checksum How to use checksums |
4677767e6ac093cf1bc58af8756e83a7f0ada47fb05c07e246e91f600076e8b3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.8.8
|
Release files / punycode-0.2.1-py3-none-any.whl
| Download URL | punycode-0.2.1-py3-none-any.whl |
|---|---|
| Size | 4.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5c6c2c1fcfdcd50752fe4e68d402495d9ae2a71ac73915bc6af39cc7ccd99eb5
|
|
BLAKE2b-256 checksum How to use checksums |
ff93c541a0b9e6838963c8b7bc3a2540bbe988cf56fe8b011ff2e1b3f255d988
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.8.8
|