Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add a new line before a capital letter?

I am writing a piece of code to get lyrics from genius.com.

I have managed to extract the code from the website but it comes out in a format where all the text is on one line.

I have used regex to add a space but cannot figure out how to add a new line. Here is my code so far:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

text_container = re.sub(r"(\w)([A-Z])", r"\1 \2", text_container.text)

This adds a space before the capital letter, but I cannot figure out how to add a new line.

It is returning [Verse 1]Leaves are fallin' down on the beautiful ground I heard a story from the man in red He said, "The leaves are fallin' down

I would like to add a new line before "He" in the command line.

Any help would be greatly appreciated.
Thanks 🙂

>Solution :

If genius.com doesn’t somehow provide a separator, it will be very hard to find a way to know what to look for.

In your example, I made a regex searching for " [A-Z]", which will find " He…". But it will also find all places where a sentence starts with " I…". Sometimes new sentences will start with "I…", but it might make new lines where there actually shouldn’t be one.

TL;DR – genius.com needs to provide some sort of separator so we know when there should be a new line.

Disclaimer: Unless I missed something in your description/example

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading