Skip to main content

Convert matching double-quotes to single-quotes or vice versa in strings and streams. Inspired by the popular to-single-quotes npm package

Project description

quotes-convert

Python Version License Tests PyPI PyPI Downloads

Convert matching double-quotes to single-quotes or vice versa in strings and streams. Inspired by the popular to-single-quotes npm package.

Features

  • Multiple input types: Convert quotes in strings and streams
  • Proper escaping: Automatically handles quote escaping and unescaping
  • Memory efficient: Process large texts with streaming without loading everything into memory
  • Zero dependencies: Lightweight with no external dependencies
  • Type safe: Full type hints for excellent IDE support

Why use this library?

Why not just use .replace('"', "'")? Because simply replacing quotes breaks strings that contain escaped quotes.

# The problem with simple replace
original = r'He said "Don\'t do it"'
broken   = original.replace('"', "'")
# Result: 'He said 'Don\'t do it'' -> Syntax Error!

# The solution: quotes-convert handles escaping correctly
from quotes_convert import single_quotes
fixed    = single_quotes(original)
# Result: 'He said "Don\'t do it"' -> Correctly preserves meaning

Installation

pip install quotes-convert

Usage Examples

Basic Usage

from quotes_convert import single_quotes, double_quotes

result = single_quotes('x = "hello"; y = "world"')
print(result)  # x = 'hello'; y = 'world'


result = double_quotes('x = "hello"; y = "world"')
print(result)  # x = "hello"; y = "world"

Handling Mixed Quotes

from quotes_convert import single_quotes, double_quotes

# Automatically escapes inner quotes
result = single_quotes('"it\'s working"')
print(result)  # 'it\'s working'

result = double_quotes("'say \"hi\"'")
print(result)  # "say \"hi\""

Processing JSON-like Strings

Useful for normalizing JSON strings or Python dict definitions.

from quotes_convert import double_quotes

json_str = "{'key': 'value', 'nested': {'inner': 'data'}}"
result = double_quotes(json_str) # {"key": "value", "nested": {"inner": "data"}}

Shell Script Processing

from quotes_convert import single_quotes

script = 'echo "Hello $USER"; grep "pattern" file.txt'
result = single_quotes(script) # echo 'Hello $USER'; grep 'pattern' file.txt

Streaming Large Texts

Process large files or streams efficiently without loading the entire content into memory.

from quotes_convert import single_quotes_stream

def line_generator():
    yield 'line 1: "hello"\n'
    yield 'line 2: "world"\n'

# Process the stream chunk by chunk
for chunk in single_quotes_stream(line_generator()):
    print(chunk, end='')

# Output:
# line 1: 'hello'
# line 2: 'world'

API Reference

Function Description
single_quotes(text: str) -> str Convert matching double-quotes to single-quotes.
double_quotes(text: str) -> str Convert matching single-quotes to double-quotes.
single_quotes_stream(stream: Iterable[str]) -> Generator[str, None, None] Convert matching double-quotes to single-quotes in a stream, yielding chunks.
double_quotes_stream(stream: Iterable[str]) -> Generator[str, None, None] Convert matching single-quotes to double-quotes in a stream, yielding chunks.

Acknowledgments

Inspired by Sindre Sorhus's to-single-quotes npm package.

Changelog

See CHANGELOG.md for a detailed list of changes and version history.

Contributing

We welcome contributions! Please see our Contributing Guide for details.

Support

If you find this library helpful:

  • ⭐ Star the repository
  • 🐛 Report issues
  • 🔀 Submit pull requests
  • 💝 Sponsor on GitHub

License

MIT © Y. Siva Sai Krishna - see LICENSE file for details.


Author's GitHubAuthor's LinkedInReport IssuesPackage on PyPI

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

quotes_convert-1.0.0.tar.gz (17.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

quotes_convert-1.0.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file quotes_convert-1.0.0.tar.gz.

File metadata

  • Download URL: quotes_convert-1.0.0.tar.gz
  • Upload date:
  • Size: 17.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for quotes_convert-1.0.0.tar.gz
Algorithm Hash digest
SHA256 e2f565234da8c194a66e23e7f796f2e782440ec47918b580e5a9ade7312bc824
MD5 eb78fff924cb69422d0c1b0aca9051db
BLAKE2b-256 f502a4e21801b7f5c0416f68130423d0605760ad849cc9a47e05d0d113da9deb

See more details on using hashes here.

File details

Details for the file quotes_convert-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: quotes_convert-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for quotes_convert-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 745ab96ac520a6207ecccc2d80232143614c85a173a54d4ad446cc6477a61a64
MD5 be8f77abd33c4551e58d460ce088835e
BLAKE2b-256 c78a75451cd9a8ee0fdb9728ce9116933e78b118d8d4b24cb2bf873e2fe2f6a8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page