Skip to content
Advertisement

Python Conditional Split

Given this string:

JavaScript

I want to split it on each new record (which starts with a date) like this:

JavaScript

Notice the extra new line delimiter between ABC and DEF? That’s the challenge I’m having. I want to preserve it without a split there. I’m thinking I need to conditionally split on any delimiter of these:

JavaScript

Is there an easy way to use re.findall this way or is there a better approach?

Thanks in advance!

Advertisement

Answer

You could split on the new line that is followed by a date with a lookahead. Something like:

JavaScript

You may be able to simplify to just a newline followed by 2 digits depending on your data: r'n(?=d{2})'

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement