I have a list like the following:
['Singh Sumer',
'Li Sheng\n',
'Hahn Vanessa',
'Ruiter Dana',
'Kleinbauer Thomas',
'Klakow Dietrich\n',
'Caselli Tommaso']
Some members have '\n' at the end. I want to remove them and put them as following members of the list.
So, I want to have this as an output:
['Singh Sumer',
'Li Sheng',
'\n',
'Hahn Vanessa'
'Ruiter Dana',
'Kleinbauer Thomas',
'Klakow Dietrich',
'\n',
'Caselli Tommaso']
I tried to get the indexes of the members who have '/n' at the end and insert '/n' to those indexes. But when I used the insert function, it replaced them with the other members of the list . Any suggestions?
CodePudding user response:
re.split with lookahead returns a list with the word and newline if present, or just the word
import re
lst = [
'Singh Sumer',
'Li Sheng\n',
'Hahn Vanessa',
'Ruiter Dana',
'Kleinbauer Thomas',
'Klakow Dietrich\n',
'Caselli Tommaso'
]
r = []
for s in lst:
r.extend(re.split(r'(?=\n)', s))
print(r)
CodePudding user response:
It is quite easy but you must keep in mind that "\n" is considered as single character, so you must consider that like only 1 letter.
That's how you can do that:
elements = ['Singh Sumer',
'Li Sheng\n',
'Hahn Vanessa',
'Ruiter Dana',
'Kleinbauer Thomas',
'Klakow Dietrich\n',
'Caselli Tommaso']
output = []
for index, element in enumerate(elements):
output.append(element.strip())
if element[-1:] == "\n":
output.insert(index 1, "\n")
print (output)
The output is:
['Singh Sumer', 'Li Sheng', '\n', 'Hahn Vanessa', 'Ruiter Dana', 'Kleinbauer Thomas', '\n', 'Klakow Dietrich', 'Caselli Tommaso']
