INNER CODE UNIT · Python
remove_multiple_lines
lefterisloukas/edgar-crawler · extract_items.py:224
def remove_multiple_lines(text: str) -> str:
"""
Replace consecutive new lines and spaces with a single new line or space.
Args:
text (str): The string containing the text.
Returns:
str: The string without multiple new lines or spaces.
"""
# Replace multiple new lines and spaces with a temporary token
text = re.sub(r"(( )*\n( )*){2,}", "#NEWLINE", text)
# Replace all new lines with a space
text = re.sub(r"\n", " ", text)
# Replace temporary token with a single new line
text = re.sub(r"(#NEWLINE)+", "\n", text).strip()
# Replace multiple spaces with a single space
text = re.sub(r"[ ]{2,}", " ", text)