Home > Blockchain >  How to remove punctuation marks from a list of strings without using libraries in Python 3.x?
How to remove punctuation marks from a list of strings without using libraries in Python 3.x?

Time:11-18

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.

CodePudding user response:

Try this Solution:

unwanted_char = [#put the punctuation marks that you want to remove here ]
for char in unwanted_char:
    data = data.replace(char, "")

CodePudding user response:

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

  • Related