Skip to main content
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.

Installation

pip install unicode-text-to-identifier

Usage

# coding=utf-8
from unicode_text_to_identifier import unicode_text_to_identifier

assert unicode_text_to_identifier(u"") == u"_"
assert unicode_text_to_identifier(u" \r\n\t") == u"_"
assert unicode_text_to_identifier(u"123abc") == u"_123abc"
assert unicode_text_to_identifier(u"&abc 123") == u"_abc_123"
assert unicode_text_to_identifier(u"  hello  world  $") == u"_hello_world__"
assert unicode_text_to_identifier(u"测试@unicode") == u"测试_unicode"

How It Works

First, get_unicode_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-text-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)

Source distribution for unicode-text-to-identifier 0.1.0a0
File Size Uploaded
unicode_text_to_identifier-0.1.0a0.tar.gz 3.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for unicode-text-to-identifier 0.1.0a0
File Interpreter ABI Platform
unicode_text_to_identifier-0.1.0a0-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 8.2 kB

Release files / unicode_text_to_identifier-0.1.0a0.tar.gz

Download URL unicode_text_to_identifier-0.1.0a0.tar.gz
Size 3.8 kB
Tags Source
SHA-256 checksum
How to use checksums
43d1734d68a8eedabf8e6517c493c1a290fe71616181e03981dab6dabe8bc627
BLAKE2b-256 checksum
How to use checksums
e4992d53738240bb15098fb34f8dea0986fee0053f9a806f3d1254d19a790ce7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release files / unicode_text_to_identifier-0.1.0a0-py2.py3-none-any.whl

Download URL unicode_text_to_identifier-0.1.0a0-py2.py3-none-any.whl
Size 4.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
08d7cdd6f7d946fe346566084dea6155837697fdb19a068812c404efb02fa1e4
BLAKE2b-256 checksum
How to use checksums
c2793cabfe2bad1f4f991708ca21cd95d68b129ff62c832ed6a29863cafc46e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release history Release notifications | RSS feed

This release

0.1.0a0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page