Pre-release
This release is a pre-release and may not be stable for production use.
A tool that converts arbitrary text (like user input or file names) into valid Python identifiers while preserving as much of the original meaning as possible.
Example Usage
from __future__ import print_function
from unicode_string_to_identifier import unicode_string_to_identifier
print(unicode_string_to_identifier(u""))
# u"_"
print(unicode_string_to_identifier(u" \r\n\t"))
# u"_"
print(unicode_string_to_identifier(u"123abc"))
# u"_123abc"
print(unicode_string_to_identifier(u"&abc 123"))
# u"_abc_123"
print(unicode_string_to_identifier(u" hello world $"))
# u"_hello_world__"
print(unicode_string_to_identifier(u"测试@unicode"))
# u"测试_unicode"
How It Works
First, get_character_type() categorizes each Unicode character into one of four types:
LETTER_OR_UNDERSCORE(Unicode category starting with 'L' or underscore_)DECIMAL_DIGIT(Unicode category 'Nd')SPACE_OR_CONTROL(Unicode categories starting with 'Z' or 'Cc')OTHER(all other characters)
Then, it implements the following conversion rules using a state machine:
- The first character must be a letter/underscore. If the first character is a digit, prepend
_to make it valid. - Subsequent valid characters (letters/underscores/digits) are kept as-is.
- Other characters are replaced with underscores, but whitespace/control character sequences are collapsed into single underscores.
- Ensures the output is non-empty (appends
_if empty).
Contributing
Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.
License
This project is licensed under the MIT License.
Release files for unicode-string-to-identifier 0.1.0a0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| unicode_string_to_identifier-0.1.0a0.tar.gz | 2.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| unicode_string_to_identifier-0.1.0a0-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Total release size: 5.3 kB
Release files / unicode_string_to_identifier-0.1.0a0.tar.gz
| Download URL | unicode_string_to_identifier-0.1.0a0.tar.gz |
|---|---|
| Size | 2.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
96410db15da4964bbbb18fb5a2e691abc7894b19ee65aa379e176df01c7774e4
|
|
BLAKE2b-256 checksum How to use checksums |
b47964dd7f91870212f1e2f24059910523b26087790e12bd2b8177efcf870be9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.10
|
Release files / unicode_string_to_identifier-0.1.0a0-py2.py3-none-any.whl
| Download URL | unicode_string_to_identifier-0.1.0a0-py2.py3-none-any.whl |
|---|---|
| Size | 2.8 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
ce8f52160a8ad5ea7a221a98d57e256a8a0b89c4b44088a4d9ae17cb61eb3277
|
|
BLAKE2b-256 checksum How to use checksums |
68995ca36ac028c38fb37b6bdd65e6ccb2d7ce9b915507039394b7aafecd8d75
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.10
|