Issue
I need to remove all punctuation marks (without being specific) and '\n' from a list of strings as for example:
l = ['``What ails you, Sister Erin, that your face\n', 'Is, like your mountains, still bedewed with tears?\n', 'As though some ancient sorrow or disgrace,\n']
output:
['What ails you Sister Erin that your face', 'Is like your mountains still bedewed with tears', 'As though some ancient sorrow or disgrace']
I'm trying to do it without using libraries.
Solution
You don't have to use any external libraries for this. The basic str
type can deal with this.
See below:
l = ['``What ails you, Sister Erin, that your face\n', 'Is, like your mountains, still bedewed with tears?\n',
'As though some ancient sorrow or disgrace,\n']
l = ["".join([char for char in line if char.isalnum() or char == " "]) for line in l]
print(l)
You just need to ignore all characters which are not alphanumeric or space
Answered By - Kris
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.