Skip to main content

textwrapre

A Python package to split text into smaller blocks based on regular expressions.

It's useful to split large texts into smaller ones that can't exceed a certain limit,

but need to be split where a certain regular expression matches.

pip install textwrapre

Usage

from textwrapre import wrapre

import regex



text = r"""

Python was created in the early 1990s by Guido van Rossum at Stichting Mathematisch 

Centrum (CWI, see https://www.cwi.nl/) in the Netherlands as a successor of a 

language called ABC. Guido remains Python’s principal author, although it includes

many contributions from others.



In 1995, Guido continued his work on Python at the Corporation for 

National Research Initiatives 

(CNRI, see https://www.cnri.reston.va.us/) in Reston, 

Virginia where he released several versions of the software.



""".strip()



# Split the text using the default regex separator

splitted = wrapre(text, blocksize=150, raisewhenlonger=True, removenewlines_from_result=True)



for s in splitted:

    print(len(s), s)



# Split the text using a custom regex separator

splitted = wrapre(text, blocksize=50, regexsep=r"[\r\n\s]+", raisewhenlonger=True, removenewlines_from_result=False, flags=regex.I)



for s in splitted:

    print(len(s), s)



# Split the text and raise an exception when the blocks are bigger than the limit

splitted = wrapre(text, blocksize=20, regexsep=r"[\r\n\s]+", raisewhenlonger=True, flags=regex.I)







85 Python was created in the early 1990s by Guido van Rossum at Stichting Mathematisch  

79 Centrum (CWI, see https://www.cwi.nl/) in the Netherlands as a successor of a  

115 language called ABC. Guido remains Python’s principal author, although it includes many contributions from others. 

99 In 1995, Guido continued his work on Python at the Corporation for  National Research Initiatives  

115 (CNRI, see https://www.cnri.reston.va.us/) in Reston,  Virginia where he released several versions of the software.

---------------------

47 Python was created in the early 1990s by Guido 

46 van Rossum at Stichting Mathematisch 

Centrum 

38 (CWI, see https://www.cwi.nl/) in the 

49 Netherlands as a successor of a 

language called 

46 ABC. Guido remains Python’s principal author, 

45 although it includes

many contributions from 

46 others.

In 1995, Guido continued his work on 

49 Python at the Corporation for 

National Research 

24 Initiatives 

(CNRI, see 

44 https://www.cnri.reston.va.us/) in Reston, 

47 Virginia where he released several versions of 

13 the software.

---------------------

Traceback (most recent call last):

  File "C:\Program Files\JetBrains\PyCharm Community Edition 2022.3.3\plugins\python-ce\helpers\pydev\pydevconsole.py", line 364, in runcode

    coro = func()

  File "<input>", line 41, in <module>

  File "C:\ProgramData\anaconda3\envs\adda\textwrapre.py", line 73, in wrapre

    raise ValueError(

ValueError: Some blocks are bigger than the limit! Try again with another separator or a bigger limit!

Parameters

def wrapre(

    text: Union[str, bytes], 

    blocksize: int, 

    regexsep: str = r"[\r\n]", 

    raisewhenlonger: bool = True, 

    removenewlines_from_result: bool = False, 

    *args, 

    **kwargs

) -> List[Union[str, bytes]]:

    """

    Split a text into blocks of a given size using a regex expression.



    :param text: the text to be splitted

    :param blocksize: the maximum size of each block

    :param regexsep: the regex expression used as separator (default: r"[\r\n]")

    :param raisewhenlonger: whether or not to raise an exception when a block is bigger than the limit (default: True)

    :param removenewlines_from_result: whether or not to remove new lines from the result (default: False)

    :param *args: additional arguments passed to the regex.compile() function

    :param **kwargs: additional keyword arguments passed to the regex.compile() function

    :return: a list of blocks

    """

Metadata

Release files for textwrapre 0.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for textwrapre 0.10
File Size Uploaded Provenance Actions
textwrapre-0.10.tar.gz 9.9 kB Without provenance Details for textwrapre-0.10.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for textwrapre 0.10
File Interpreter ABI Platform Actions
textwrapre-0.10-py3-none-any.whl Python 3 none any Details for textwrapre-0.10-py3-none-any.whl

Total release size: 21.5 kB

Release files / textwrapre-0.10.tar.gz

Download URL textwrapre-0.10.tar.gz
Size 9.9 kB
Tags Source
SHA-256 checksum
How to use checksums
cbbdf1f89a37c333c2f087430f3cfe18880ffaa5935ec703f59dcb3168c037b9
BLAKE2b-256 checksum
How to use checksums
209fb139d5de9374218fa8345f1d732a5e30b8f93239ae8d19d2f1c775640d40
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.10

Release files / textwrapre-0.10-py3-none-any.whl

Download URL textwrapre-0.10-py3-none-any.whl
Size 11.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
852e5af25d8fd1ead28f396e4c62cf848afa62a17ca24f60c1694ec81db08e18
BLAKE2b-256 checksum
How to use checksums
6bf035eed9a7174c59d8d94630b2d30a96bf1ed019e2e3847eb23b5dcd978b13
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.10

Security for textwrapre 0.10

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.10 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page