Punycode Converter Library for Python
Project description
punycode
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.
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file punycode-0.2.0.tar.gz
.
File metadata
- Download URL: punycode-0.2.0.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ae1ff1ca080a35dc4b71c5c9f0c0dda5dd4b94e11ef655202996c8a7bae85596 |
|
MD5 | ccce2757faa37b34432545f929a96da5 |
|
BLAKE2b-256 | 7d56d70ef136a6526df9b5d30d5394106ca40ed5b11a36ebbc8c5090bc0fc05b |
File details
Details for the file punycode-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: punycode-0.2.0-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3915699d4baa1fb04962f2b0218b96008bd77c7cff11aa8da3486d1258199e26 |
|
MD5 | b0c58b8f8e5c579c5c69cdeb6ca69d91 |
|
BLAKE2b-256 | b173944dfb8c4665a26956111a4d3bdd2744d85f723859f331a2cd69c830862e |