Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python split string but keep delimiter

In python I can easily read a file line by line into a set, just be using:

file = open("filename.txt", 'r')
content = set(file)

each of the elements in the set consists of the actual line and also the trailing line-break.

Now I have a string with multiple lines, which I want to compare to the content by using the normal set operations.

Is there any way of transforming a string into a set just the same way, such, that it also contains the line-breaks?


Edit:

The question "In Python, how do I split a string and keep the separators?" deals with a similar problem, but the answer doesn't make it easy to adopt to other use-cases.

import re
content = re.split("(\n)", string)

doesn't have the expected effect.

like image 259
white_gecko Avatar asked Jan 18 '26 15:01

white_gecko


2 Answers

The str.splitlines() method does exactly what you want if you pass True as the optional keepends parameter. It keeps the newlines on the end of each line, and doesn't add one to the last line if there was no newline at the end of the string.

text = "foo\nbar\nbaz"
lines = text.splitlines(True)
print(lines) # prints ['foo\n', 'bar\n', 'baz']
like image 119
Blckknght Avatar answered Jan 20 '26 06:01

Blckknght


Here's a simple generator that does the job:

content = set(e + "\n" for e in s.split("\n"))

This solution adds an additional newline at the end though.

like image 32
Francisco Avatar answered Jan 20 '26 07:01

Francisco



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!