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 text-to-identifier

Usage

# coding=utf-8
from text_to_identifier import text_to_identifier

assert text_to_identifier(u"") == u"_"
assert text_to_identifier(u" \r\n\t") == u"_"
assert text_to_identifier(u"123abc") == u"_123abc"
assert text_to_identifier(u"&abc 123") == u"_abc_123"
assert text_to_identifier(u"  hello  world  $") == u"_hello_world__"
assert 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.

Metadata

Release files for text-to-identifier 0.1.0a1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for text-to-identifier 0.1.0a1
File Size Uploaded Provenance Actions
text_to_identifier-0.1.0a1.tar.gz 3.8 kB Without provenance Details for text_to_identifier-0.1.0a1.tar.gz

Built distribution (wheel)

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

Total release size: 8.1 kB

Release files / text_to_identifier-0.1.0a1.tar.gz

Download URL text_to_identifier-0.1.0a1.tar.gz
Size 3.8 kB
Tags Source
SHA-256 checksum
How to use checksums
4125733b3e6c2888fbf7be0f38ec5f2929cbf35c29f030c1d2b3aba179070e4d
BLAKE2b-256 checksum
How to use checksums
6ccc6de95a878faa5d62c8e93aa6b443eb742b14c037e446e8efbb30c3d277ed
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.2

Release files / text_to_identifier-0.1.0a1-py2.py3-none-any.whl

Download URL text_to_identifier-0.1.0a1-py2.py3-none-any.whl
Size 4.3 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
3b30e9cfa2b8c8b3f97dfd679927623ce8d9194964265d7d7d147c4cbf52d9ad
BLAKE2b-256 checksum
How to use checksums
836943ef835a21c206b52ac891ade144e9935169c14b5a2e21844367d76fa502
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.2

Security for text-to-identifier 0.1.0a1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release
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