split¶
Quote-aware string splitting utilities.
This module provides functions for splitting strings while respecting quoted sections and escaped characters. It supports custom quote maps, nesting rules, and unescaping, making it suitable for parsing shell-like or configuration syntax.
Classes:
| Name | Description |
|---|---|
_Skipper |
Tracks quote positions during forward scanning. |
SplitContext |
Named tuple holding quote mapping and regex patterns. |
Main APIs
_chars_regex, _specials_regex, _split_forward, make_context, _to_bytes, split, unquote
make_context(quote_map)
¶
Creates a SplitContext from a quote mapping dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
quote_map
|
A dict mapping opening quote characters to closing characters. |
required |
Returns:
| Type | Description |
|---|---|
|
A |
|
|
detection. |
Source code in py_misc_utils/split.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |
split(data, split_rx, quote_ctx=None, unescape=False)
¶
Splits data while respecting quoted sections and escaped characters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
A string or bytes object to split. |
required | |
split_rx
|
A string or compiled regex pattern for the split delimiter. |
required | |
quote_ctx
|
An optional SplitContext defining quote characters. Defaults to a context with common quote pairs ("'", "'", "`", "()", "{}", "[]", "<>"). |
None
|
|
unescape
|
If True, backslash-escaped characters are unescaped during splitting. |
False
|
Returns:
| Type | Description |
|---|---|
|
A tuple of parts (strings if input was str, bytes if input was bytes). |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If quotes are unmatched at the end of the data. |
Example
from py_misc_utils import split as sp
print(sp.split('a, "b, c", d', ',')) # ('a', ' "b, c"', ' d')
# An escaped separator is honored only with unescape=True:
esc_sep = ',' + chr(92) + ','
print(sp.split('x' + esc_sep + 'y', ',', unescape=True)) # ('x,y',)
print(sp.split('x' + esc_sep + 'y', ',')) # ('x\,y',)
# A custom quote context, e.g. only parentheses are special:
ctx = sp.make_context({'(': ')'})
print(sp.split('a, (b, c)', ',', quote_ctx=ctx)) # ('a', ' (b, c)')
Source code in py_misc_utils/split.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | |
unquote(data, quote_map=None)
¶
Removes matching quotes from the beginning and end of a string.
If the string starts and ends with a matching quote pair, the quotes are removed and any escaped occurrences of the closing quote are unescaped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
The string to unquote. |
required | |
quote_map
|
An optional dict mapping opening quotes to closing quotes.
Defaults to the module-level |
None
|
Returns:
| Type | Description |
|---|---|
|
The string with surrounding quotes removed, or the original string if no |
|
|
matching quote pair is found. |
Example
from py_misc_utils import split as sp
print(sp.unquote('"quoted"')) # quoted
print(sp.unquote('[lst]')) # lst
print(sp.unquote('plain')) # plain
Source code in py_misc_utils/split.py
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | |