itsjube

Untitled

Nov 5th, 2016
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. #removes punctuation from provided string
  2. #but preserves punctuation between digits (I am allowing ALL punctuation)
  3. def remove_punctuation(string):
  4. results = ""
  5. previous = ""
  6. next = ""
  7. for i, c in enumerate(string):
  8. if (i > 0):
  9. previous = string[i - 1]
  10. if (i + 1 != len(string)):
  11. next = string[i + 1]
  12. if (not previous.isdigit() or not next.isdigit()): # if the previous and the next one are not digits
  13. if (c not in punctuation_list):
  14. results += c
  15. else:
  16. results += c
  17. return results
  18.  
  19. #open_document()
  20. punctuation_list = ['\\','/',':',',','...','(',')','.','?','!','\"',';','\'','–','’']
  21. string = "major role — international and — international long-lived—many users’ consumer’s 2004/8ec"
  22.  
  23. print(remove_punctuation(string))
Advertisement
Add Comment
Please, Sign In to add comment