https://github.com/jwodder/linesep
Handling lines with arbitrary separators
https://github.com/jwodder/linesep
available-on-pypi delimiters line-break line-ending linebreak newline paragraphs python separator
Last synced: about 1 month ago
JSON representation
Handling lines with arbitrary separators
- Host: GitHub
- URL: https://github.com/jwodder/linesep
- Owner: jwodder
- License: mit
- Created: 2016-05-12T13:44:52.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2024-04-22T12:28:22.000Z (about 1 year ago)
- Last Synced: 2024-04-22T13:45:54.610Z (about 1 year ago)
- Topics: available-on-pypi, delimiters, line-break, line-ending, linebreak, newline, paragraphs, python, separator
- Language: Python
- Homepage:
- Size: 159 KB
- Stars: 1
- Watchers: 3
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.rst
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
|repostatus| |ci-status| |coverage| |pyversions| |license|
.. |repostatus| image:: https://www.repostatus.org/badges/latest/active.svg
:target: https://www.repostatus.org/#active
:alt: Project Status: Active — The project has reached a stable, usable
state and is being actively developed... |ci-status| image:: https://github.com/jwodder/linesep/actions/workflows/test.yml/badge.svg
:target: https://github.com/jwodder/linesep/actions/workflows/test.yml
:alt: CI Status.. |coverage| image:: https://codecov.io/gh/jwodder/linesep/branch/master/graph/badge.svg
:target: https://codecov.io/gh/jwodder/linesep.. |pyversions| image:: https://img.shields.io/pypi/pyversions/linesep.svg
:target: https://pypi.org/project/linesep.. |license| image:: https://img.shields.io/github/license/jwodder/linesep.svg
:target: https://opensource.org/licenses/MIT
:alt: MIT License`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.8 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:
.. code:: python
with open('text.txt') as fp:
for entry in linesep.read_separated(fp, '\n---\n'):
...Parsing output from ``find -print0``:
.. code:: python
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:
.. code:: python
for entry in linesep.read_preceded(fp, '\x1E'):
try:
obj = json.loads(entry)
except ValueError:
pass
else:
yield objRead from a text file one paragraph at a time:
.. code:: python
with open("my-novel.txt") as fp:
for paragraph in linesep.read_paragraphs(fp):
...Split input from an ``anyio.TextReceiveStream`` on newlines:
.. code:: python
async with anyio.TextReceiveStream( ... ) as stream:
splitter = linesep.UnicodeNewlineSplitter()
async for line in splitter.aitersplit(stream):
print(line)