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

License Tests Python PyPI PyPI Downloads Documentation Interactive Playground

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

🚀 Try it interactively in your browser! Test the library with our Interactive Playground - no installation required.

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

Contributions are welcome! Please read our Contributing Guide for details on our code of conduct, development setup, and the process for submitting pull requests.

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 PyPIPackage DocumentationPackage Playground

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.1.0.tar.gz (751.9 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.1.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: quotes_convert-1.1.0.tar.gz
  • Upload date:
  • Size: 751.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","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.1.0.tar.gz
Algorithm Hash digest
SHA256 22c09f5d5e66c765f458eaddc15e32bcd1ba15e211eca6e9ba78230ae20c5633
MD5 b98b84e31d326d23c1bbbd7603b445e7
BLAKE2b-256 a4feb945561f27361d49555c6a17aff4221b13a4571d264d1888266614ea4aff

See more details on using hashes here.

File details

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

File metadata

  • Download URL: quotes_convert-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 191652631f8d3e2e59d4348cdd2558a9b3dd141d2a67af490223fbdfad853d39
MD5 4fe745c7f8caa9f3a8b38a3bbf74310c
BLAKE2b-256 d520b1599e5b1ca9b8372c906b3e9ca1615bb9a141bc93c9f17d3495ad4f9c8b

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