I’m trying to perform a string split on a set of somewhat irregular data that looks something like:
\n\tName: John Smith
\n\t Home: Anytown USA
\n\t Phone: 555-555-555
\n\t Other Home: Somewhere Else
\n\t Notes: Other data
\n\tName: Jane Smith
\n\t Misc: Data with spaces
I’d like to convert this into a tuple/dict where I later will split on the colon :, but first I need to get rid of all the extra whitespace. I’m guessing a regex is the best way but I can’t seem to get one that works, below is my attempt.
data_string.split('\n\t *')
Just use .strip(), it removes all whitespace for you, including tabs and newlines, while splitting. The splitting itself can then be done with
data_string.splitlines():Output:
You can even inline the splitting on
:as well now: