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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

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

Hashes for linesep-0.5.0.tar.gz
Algorithm Hash digest
SHA256 30ac5645f26cf368772ea092f65487d8caaa3040996bf0fde630035bc82459a4
MD5 d0aa82667043b27bbca1c0814820e341
BLAKE2b-256 3697a07dcd86428a6b2af2e38faeacef443ba92d9edd88e2e4c92ecf4ac9ce83

See more details on using hashes here.

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

Hashes for linesep-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 56c633dae69361be5daba9f7f8083261187efb3c0f2d436cdd50459aa69bd167
MD5 b6fd7df40ceb5d9290706d38b1041aff
BLAKE2b-256 716efb445b45037a1941ed41cb5b86b6e519e0af90f00e94819609eda592d052

See more details on using hashes here.

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