Handling lines with arbitrary separators
Project description
GitHub | PyPI | Documentation | Issues | Changelog
linesep provides basic functions & classes for reading, writing, splitting, & joining text with custom separators that can occur either before, between, or after the segments they separate.
Installation
linesep requires Python 3.7 or higher. Just use pip for Python 3 (You have pip, right?) to install:
python3 -m pip install linesep
Examples
Reading sections separated by a “---” line:
with open('text.txt') as fp:
for entry in linesep.read_separated(fp, '\n---\n'):
...
Parsing output from find -print0:
find = subprocess.Popen(
['find', '/', '-some', '-complicated', '-condition', '-print0'],
stdout=subprocess.PIPE,
)
for filepath in linesep.read_terminated(find.stdout, '\0'):
...
A poor man’s JSON Text Sequence parser:
for entry in linesep.read_preceded(fp, '\x1E'):
try:
obj = json.loads(entry)
except ValueError:
pass
else:
yield obj
Read from a text file one paragraph at a time:
with open("my-novel.txt") as fp:
for paragraph in linesep.read_paragraphs(fp):
...
Split input from an anyio.TextReceiveStream on newlines:
async with anyio.TextReceiveStream( ... ) as stream:
splitter = linesep.UnicodeNewlineSplitter()
async for line in splitter.aitersplit(stream):
print(line)
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
Built Distribution
File details
Details for the file linesep-0.5.0.tar.gz
.
File metadata
- Download URL: linesep-0.5.0.tar.gz
- Upload date:
- Size: 24.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 30ac5645f26cf368772ea092f65487d8caaa3040996bf0fde630035bc82459a4 |
|
MD5 | d0aa82667043b27bbca1c0814820e341 |
|
BLAKE2b-256 | 3697a07dcd86428a6b2af2e38faeacef443ba92d9edd88e2e4c92ecf4ac9ce83 |
File details
Details for the file linesep-0.5.0-py3-none-any.whl
.
File metadata
- Download URL: linesep-0.5.0-py3-none-any.whl
- Upload date:
- Size: 12.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 56c633dae69361be5daba9f7f8083261187efb3c0f2d436cdd50459aa69bd167 |
|
MD5 | b6fd7df40ceb5d9290706d38b1041aff |
|
BLAKE2b-256 | 716efb445b45037a1941ed41cb5b86b6e519e0af90f00e94819609eda592d052 |