0
Как убрать все знаки препинания из текста? How do I remove all punctuation marks from the text?
Предположим, дан текст: Алексей шел по лесу и собирал грибы.Собрав корзину, он решил вернуться домой, но тут началась гроза. Алексей был испепелён молнией. Как из данного текста удалить все знаки препинания, вроде пробелов, запятых, точек, превратив текст в единую строку? TRANSLATED: Suppose the text is given: Alexey was walking through the forest and picking mushrooms.After collecting the basket, he decided to return home, but then a thunderstorm began. Alexey was incinerated by lightning. How can I remove all punctuation marks, such as spaces, commas, and dots from this text, turning the text into a single line?
1 Resposta
0
Here, I've done trial and errors and I think this is the best I can do.
The only punctuations I see in the given text are "." and "," so I assigned them as "puncts"
I then used an "if not in" to tell Python what to pick from "text" (if what is inside "text" are not what is inside "puncts", then pick them)
Normally this is not easy since there are so many characters inside the text which means, manually, I should make Python to check whether each characters are equal to what is inside "puncts" one by one. But thanks to "for loop", I don't need to do that and just let Python do that for each characters one by one instead of me!
text="""Alexey was walking through the forest and picking mushrooms.After collecting the basket, he decided to return home, but then a thunderstorm began. Alexey was incinerated by lightning."""
puncts=["." , ","]
fixed=""
for characters in text:
if characters not in puncts:
fixed += characters
print(fixed)