跳转到主要内容

处理任意分隔符的行

项目描述

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 | 文档 | 问题 | 变更日志

linesep 提供了用于读取、写入、分割和连接文本的基本函数和类,这些文本可以具有自定义分隔符,这些分隔符可以出现在分隔段之前、之间或之后。

安装

linesep 需要 Python 3.7 或更高版本。只需使用 Python 3 的 pip 安装即可(你有 pip,对吧?)

python3 -m pip install linesep

示例

按“---”行分隔的节阅读

with open('text.txt') as fp:
    for entry in linesep.read_separated(fp, '\n---\n'):
        ...

解析 find -print0 的输出

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

一个简陋的 JSON 文本序列 解析器

for entry in linesep.read_preceded(fp, '\x1E'):
    try:
        obj = json.loads(entry)
    except ValueError:
        pass
    else:
        yield obj

逐段从文本文件中读取

with open("my-novel.txt") as fp:
    for paragraph in linesep.read_paragraphs(fp):
        ...

在换行符上从 anyio.TextReceiveStream 中分割输入

async with anyio.TextReceiveStream( ... ) as stream:
    splitter = linesep.UnicodeNewlineSplitter()
    async for line in splitter.aitersplit(stream):
        print(line)

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源代码分发

linesep-0.5.0.tar.gz (24.0 kB 查看哈希值)

上传时间 源代码

构建分发

linesep-0.5.0-py3-none-any.whl (12.2 kB 查看哈希值)

上传时间 Python 3

支持者