Skip to main content

Handling lines with arbitrary separators

Project description

Project Status: Active — The project has reached a stable, usable state and is being actively developed. CI Status https://codecov.io/gh/jwodder/linesep/branch/master/graph/badge.svg https://img.shields.io/pypi/pyversions/linesep.svg 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.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

linesep-0.5.0.tar.gz (24.0 kB view hashes)

Uploaded Source

Built Distribution

linesep-0.5.0-py3-none-any.whl (12.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page