Skip to content

Repository files navigation

Project Status: Active — The project has reached a stable, usable state and is being actively developed.CI StatuscoveragepyversionsMIT 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.10 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:

withopen('text.txt') asfp:
forentryinlinesep.read_separated(fp, '\n---\n'):
...

Parsing output from find -print0:

find=subprocess.Popen(
['find', '/', '-some', '-complicated', '-condition', '-print0'],
stdout=subprocess.PIPE,
)
forfilepathinlinesep.read_terminated(find.stdout, '\0'):
...

A poor man's JSON Text Sequence parser:

forentryinlinesep.read_preceded(fp, '\x1E'):
try:
obj=json.loads(entry)
exceptValueError:
passelse:
yieldobj

Read from a text file one paragraph at a time:

withopen("my-novel.txt") asfp:
forparagraphinlinesep.read_paragraphs(fp):
...

Split input from an anyio.TextReceiveStream on newlines:

asyncwithanyio.TextReceiveStream( ... ) asstream:
splitter=linesep.UnicodeNewlineSplitter()
asyncforlineinsplitter.aitersplit(stream):
print(line)

About

Handling lines with arbitrary separators

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages