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
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file quotes_convert-1.2.0.tar.gz.
File metadata
- Download URL: quotes_convert-1.2.0.tar.gz
- Upload date:
- Size: 745.5 kB
- Tags: Source
- Uploaded using 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}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f799974fa28c8b0167706d1e29d57f0362456b0f6452b0594809c4f31c22f232
|
|
| MD5 |
d3ce89ed70a98fd39b87c490b8a0cd39
|
|
| BLAKE2b-256 |
10aed97df52143122f153ce6f34b6a7cd3942b2e84d56afc643aaa7d3fda3198
|
File details
Details for the file quotes_convert-1.2.0-py3-none-any.whl.
File metadata
- Download URL: quotes_convert-1.2.0-py3-none-any.whl
- Upload date:
- Size: 9.2 kB
- Tags: Python 3
- Uploaded using 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}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f84e72bdc0fd8ab9737187077de288f70d9e7dcde21457e089f17ca4398e14db
|
|
| MD5 |
0e5424617ae2bf7f433047fa36e569e6
|
|
| BLAKE2b-256 |
8194b1a133fac577b9dc373a35ccc33f281b59ef01f6fb6798e3a59dc8fac8c8
|