Python Forum
Removing punctuation from strings in lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Removing punctuation from strings in lists
#1
Hi,

Stdin reads any text entered in, into a list, so a limerick, 5 lines of text, will be read into a list of 5 comma separated list items. How can I strip out the punctuation from this?

lines = ["There was an old man from Peru,", "Who said he had nothing to do,", "So he sat on the stairs,", "And counted he hairs,", "And found he had seventy-two"]
     
I can strip out the punctuation if it is just a simple string:
import re
lines = "There was an old man from Peru,", "Who said he had nothing to do,", "So he sat on the stairs,", "And counted he hairs,", "And found he had seventy-two"
lines = re.sub(r'[^\w\s]','',lines)
But I can't figure out how to do this with a list, without then looping through the list into one string, which I am then only going to turn back into a list anyway. Is there a nice way to do this please?

Found it:

 lines = ["There was an old man from Peru,", "Who said he had nothing to do,", "So he sat on the stairs,", "And counted he hairs,", "And found he had seventy-two"]

 input_text = ''.join(lines).lower()
 input_text = re.sub(r'[,.:@#?!&$]', ' ', input_text)
This works well, looks like some regex studying is required.
Reply
#2
newlines = []
for item in lines:
   newlines.append(item.strip(','))
lines = newlines
Reply
#3
(May-21-2017, 04:57 PM)Larz60+ Wrote:
newlines = []
for item in lines:
   newlines.append(item.strip(','))
lines = newlines

Why not just list comprehension
lines = [l.strip(',') for l in lines]
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#4
sounds good to me .. old habit
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 758 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,758 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,368 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  string.punctuation for languages like French or German Leo978 1 2,314 Jun-06-2020, 09:09 AM
Last Post: DeaD_EyE
  Where does string.punctuation come from? Mark17 5 2,974 Sep-16-2019, 04:42 PM
Last Post: Mark17
  Finding multiple strings between the two same strings Slither 1 2,514 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,264 Mar-20-2019, 08:01 PM
Last Post: stillsen
  lists, strings, and byte strings Skaperen 2 4,217 Mar-02-2018, 02:12 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020