Skip to content

Recipes#

This section provides practical examples of using this library and code snippets.

Data Analysis#

In data analysis workflows, converting column names to a consistent format, such as snake, can significantly simplify access to data and simplify its manipulation:

import csv

from textcase import ACRONYM, Boundary, snake

SCOPE = Boundary.from_delimiter("::")

with open("input.csv") as file:
    reader = csv.DictReader(file)
    reader.fieldnames = tuple(snake(column, boundaries=(ACRONYM, SCOPE)) for column in reader.fieldnames or ())

    print(*reader.fieldnames, sep=",")

    for row in reader:
        print(*map(row.get, reader.fieldnames), sep=",")
1
2
3
4
5
User::Id,User::Name,User::HTTPEndpoint
1,John Doe,/api/users/1
2,Jane Smith,/api/users/2
3,Alice Johnson,/api/users/3
4,Bob Brown,/api/users/4
1
2
3
4
5
user_id,user_name,user_http_endpoint
1,John Doe,/api/users/1
2,Jane Smith,/api/users/2
3,Alice Johnson,/api/users/3
4,Bob Brown,/api/users/4

By converting column names to a more accessible format, you can later access data using row["user_id"] instead of row["User::Id"]. This approach simplifies the syntax and improves code readability, making it easier to work with the data.

Filenames Conversion#

Different operating systems, such as Windows, macOS, and Linux, have distinct rules governing filenames. Issues can arise from spaces, special characters, and case sensitivity when sharing files across platforms. Converting filenames to a standardized format, such as kebab, enhances compatibility and minimizes the risk of errors when accessing files in various environments.

Consider the following example filenames that users might upload to a web application for conversion into different file formats (e.g., Markdown to PDF, CSV to Excel):

  • "TODO #3 (Draft).md"
  • "BigQuery.csv"
  • "Résumé2025.docx"
  • "LLMCache: Кеширование LLM запросов.pptx"
  • "The Python 3 Standard Library by Example.pdf"

For instance, the filename "TODO #3 (Draft).md" contains spaces and special characters, including "#", which is interpreted as an anchor in URLs. This can result in broken links when generating download URLs. For example, the URL "https://example.com/download/TODO%20#3%20(Draft).md" may cause the browser to misinterpret the link, preventing users from accessing the intended file.

To address this issue, you can use this library to convert filenames into a more compatible format:

from pathlib import Path

import textcase

filenames = (
    "TODO #3 (Draft).md",
    "BigQuery.csv",
    "Résumé2025.DOCX",
    "LLMCache: Кеширование LLM запросов.pptx",
    "The Python 3 Standard Library by Example.pdf",
)

new_filenames = (textcase.kebab(path.stem) + path.suffix.lower() for filename in filenames if (path := Path(filename)))

for filename, new_filename in zip(filenames, new_filenames):
    print(filename, new_filename, sep=",")
Original Filename New Filename
TODO #3 (Draft).md todo-3-draft.md
BigQuery.csv big-query.csv
Résumé2025.DOCX résumé-2025.docx
LLMCache: Кеширование LLM запросов.pptx llm-cache-кеширование-llm-запросов.pptx
The Python 3 Standard Library by Example.pdf the-python-3-standard-library-by-example.pdf

This conversion process ensures that filenames are safe for use across different platforms.