I was trying to make a compression algorithm and a compressor that uses it in python.
While doing so, I saw that when compressing some example text, newlines are gone.
After some debugging, I found out that the split() function was removing the newlines.
I was converting a string to a list and a list to string so many times, but this time, the split() function removed all the newlines. If it would be a string:
i
hate foo
bar why do people use it what does it even mean
but after calling split(), it becomes:
['i', 'hate', 'foo', 'bar', 'why', 'do', 'people', 'use', 'it', 'what', 'does', 'it', 'even', 'mean']
>Solution :
split() without argument given does split at any whitespaces, newline (\n) is one of whitespaces. If you want to split only at space characters, then provide " " as split 1st argument that is
text = '''i
hate foo
bar why do people use it what does it even mean'''
elements = text.split(" ")
print(elements)
output
['i\nhate', 'foo\nbar', 'why', 'do', 'people', 'use', 'it', 'what', 'does', 'it', 'even', 'mean']