Hướng dẫn how do you remove punctuation marks in python? - làm cách nào để xóa dấu chấm câu trong python?
Nhiều lần khi làm việc với các chuỗi Python, chúng tôi có một vấn đề trong đó chúng tôi cần loại bỏ một số ký tự nhất định khỏi chuỗi. Điều này có thể có các ứng dụng trong tiền xử lý dữ liệu trong lĩnh vực khoa học dữ liệu và cả trong lập trình hàng ngày. Hãy để thảo luận về những cách nhất định mà chúng ta có thể thực hiện nhiệm vụ này bằng Python. Show
Phương pháp 1: Xóa dấu câu từ chuỗi có dịchHai đối số đầu tiên cho phương thức String.Translate là các chuỗi trống và đầu vào thứ ba là danh sách python của dấu câu cần được xóa. Điều này hướng dẫn phương pháp Python để loại bỏ dấu câu từ một chuỗi. Đây là một trong những cách tốt nhất để dải dấu câu từ một chuỗi.best ways to strip punctuation from a string. Python3
The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks0 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks1 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks2 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks3 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks1 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks2 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks6 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks7 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks8 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks9 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks0 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks1 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks2 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks3 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks4 Output: Gfg is best for Geeks Phương pháp 2: Xóa dấu câu từ một chuỗi với vòng lặp PythonPython loopĐây là cách vũ phu trong đó nhiệm vụ này có thể được thực hiện. Trong đó, chúng tôi kiểm tra các dấu chấm câu bằng cách sử dụng một chuỗi thô có chứa dấu chấm câu và sau đó chúng tôi xây dựng một chuỗi loại bỏ các dấu câu đó. Python3The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks1 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks2 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks7 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks3 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks8 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks0 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks1 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks2 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks3 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks2 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks5 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks6 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks7 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks8 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks7 0 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks6 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks7 3 4The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks1 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks2 7The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks3 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks8 0 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks1 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks2 Output: The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks Phương pháp 3: Xóa dấu câu từ một chuỗi với Regex & NBSP;Phần của việc thay thế bằng dấu câu cũng có thể được thực hiện bằng Regex. Trong đó, chúng tôi thay thế tất cả các dấu câu bằng một chuỗi trống bằng cách sử dụng một regex nhất định. Python3
4The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks1 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks2 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks7 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks3 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks8 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks0 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks1 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks2 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks5 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks6 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks7 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks8 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks7 0 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks6 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks7 3
The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks 3The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks 8s.translate(str.maketrans('', '', string.punctuation)) 0 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks 1 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks 2Python3The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks1 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks2 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks7 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks3 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks8 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks0 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks1 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks2 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks3 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks2 3The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks2 5The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks5 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks6 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks7 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks8 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks7 0 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks6 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks7 3 4 3The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks1 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks2 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks00 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks7 0 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks6 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks7 3
The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks3 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks8 0 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks1 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks2 Phương pháp 3: Xóa dấu câu từ một chuỗi với Regex & NBSP;O(n) Phần của việc thay thế bằng dấu câu cũng có thể được thực hiện bằng Regex. Trong đó, chúng tôi thay thế tất cả các dấu câu bằng một chuỗi trống bằng cách sử dụng một regex nhất định.O(n)
4
3The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks2 5 6 7
The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks3 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks8 0 The original string is : Gfg, is best : for ! Geeks ; The string after punctuation filter : Gfg is best for Geeks1 2Nếu tốc độ không phải là một lo lắng, mặc dù có một lựa chọn khác là:
Điều này nhanh hơn s.replace với mỗi char, nhưng sẽ không thực hiện cũng như các phương pháp python không pure như regexes hoặc string.translate, như bạn có thể thấy từ các thời gian dưới đây.Đối với loại vấn đề này, thực hiện nó ở mức thấp nhất có thể được đền đáp. Mã thời gian:
Điều này cho kết quả sau: |