quotes-convert
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
- Command-line interface: Optional CLI tool for file and stream processing
- Proper escaping: Automatically handles quote escaping and unescaping
- Memory efficient: Process large texts with streaming without loading everything into memory
- Zero dependencies: Lightweight core library 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 nested quotes.
# The problem with simple replace
original = '"result = \'test\'"'
broken = original.replace('"', "'")
# Result: 'result = 'test'' -> Broken! Unmatched quotes
# The solution: quotes-convert handles escaping correctly
from quotes_convert import single_quotes
fixed = single_quotes(original)
# Result: 'result = \'test\'' -> Fixed! Properly escaped
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'
Command-Line Interface
The CLI tool allows you to convert quotes in files and streams directly from the command line.
Installation
pip install quotes-convert[cli]
Quick Examples
# Convert to single quotes
quotes-convert -s file.py
echo 'x = "hello"' | quotes-convert -s
# Convert to double quotes
quotes-convert -d file.py
# Edit file in place
quotes-convert -s -i file.py
# Check version
quotes-convert --version
For complete documentation including batch processing, shell integration, and advanced usage, see the CLI Guide.
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 GitHub • Author's LinkedIn • Report Issues • Package on PyPI • Package Documentation • Package Playground
Metadata
Release files for quotes-convert 1.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| quotes_convert-1.2.0.tar.gz | 745.5 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| quotes_convert-1.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 754.7 kB
Release files / quotes_convert-1.2.0.tar.gz
| Download URL | quotes_convert-1.2.0.tar.gz |
|---|---|
| Size | 745.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f799974fa28c8b0167706d1e29d57f0362456b0f6452b0594809c4f31c22f232
|
|
BLAKE2b-256 checksum How to use checksums |
10aed97df52143122f153ce6f34b6a7cd3942b2e84d56afc643aaa7d3fda3198
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","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}
|
Release files / quotes_convert-1.2.0-py3-none-any.whl
| Download URL | quotes_convert-1.2.0-py3-none-any.whl |
|---|---|
| Size | 9.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f84e72bdc0fd8ab9737187077de288f70d9e7dcde21457e089f17ca4398e14db
|
|
BLAKE2b-256 checksum How to use checksums |
8194b1a133fac577b9dc373a35ccc33f281b59ef01f6fb6798e3a59dc8fac8c8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","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}
|
Security for quotes-convert 1.2.0
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.