Hướng dẫn remove all periods from string python - xóa tất cả các dấu chấm khỏi chuỗi python

Chuỗi trong Python là bất biến [không thể thay đổi]. Bởi vì điều này, hiệu ứng của

import re
line = re.sub['[!@#$]', '', line]
1 chỉ là để tạo ra một chuỗi mới, thay vì thay đổi cái cũ. Bạn cần phải sửa đổi [gán] nó cho
import re
line = re.sub['[!@#$]', '', line]
2 để có biến đó lấy giá trị mới, với các ký tự đó bị xóa.

Ngoài ra, cách bạn đang làm, nó sẽ là loại chậm, tương đối. Nó cũng có khả năng là một chút khó hiểu đối với các pythonators có kinh nghiệm, những người sẽ nhìn thấy một cấu trúc được lồng gấp đôi và suy nghĩ trong một khoảnh khắc mà một cái gì đó phức tạp hơn đang diễn ra.

Bắt đầu trong Python 2.6 và các phiên bản Python 2.x mới hơn *, thay vào đó bạn có thể sử dụng

import re
line = re.sub['[!@#$]', '', line]
3, [xem Python 3 Trả lời bên dưới]:Python 3 answer below]:

line = line.translate[None, '!@#$']

hoặc thay thế biểu thức thông thường bằng

import re
line = re.sub['[!@#$]', '', line]
4

import re
line = re.sub['[!@#$]', '', line]

Các nhân vật được đặt trong ngoặc tạo thành một lớp nhân vật. Bất kỳ ký tự nào trong

import re
line = re.sub['[!@#$]', '', line]
2 nằm trong lớp đó đều được thay thế bằng tham số thứ hai thành
import re
line = re.sub['[!@#$]', '', line]
6: một chuỗi trống.

Python 3 Trả lời

Trong Python 3, chuỗi là Unicode. Bạn sẽ phải dịch một chút khác nhau. Kevpie đề cập đến điều này trong một bình luận về một trong những câu trả lời và nó được ghi nhận trong tài liệu cho

import re
line = re.sub['[!@#$]', '', line]
3.

Khi gọi phương thức

import re
line = re.sub['[!@#$]', '', line]
8 của chuỗi Unicode, bạn không thể vượt qua tham số thứ hai mà chúng tôi đã sử dụng ở trên. Bạn cũng không thể vượt qua
import re
line = re.sub['[!@#$]', '', line]
9 là tham số đầu tiên. Thay vào đó, bạn vượt qua một bảng dịch [thường là từ điển] là tham số duy nhất. Bảng này ánh xạ các giá trị thứ tự của các ký tự [nghĩa là kết quả của việc gọi
translation_table = dict.fromkeys[map[ord, '!@#$'], None]
unicode_line = unicode_line.translate[translation_table]
0 trên chúng] đến các giá trị thứ tự của các ký tự sẽ thay thế chúng, hoặc tinh thần cho chúng ta.

Vì vậy, để thực hiện các điệu nhảy trên với một chuỗi unicode, bạn sẽ gọi một cái gì đó như

translation_table = dict.fromkeys[map[ord, '!@#$'], None]
unicode_line = unicode_line.translate[translation_table]

Ở đây

translation_table = dict.fromkeys[map[ord, '!@#$'], None]
unicode_line = unicode_line.translate[translation_table]
2 và
translation_table = dict.fromkeys[map[ord, '!@#$'], None]
unicode_line = unicode_line.translate[translation_table]
3 được sử dụng để tạo ngắn gọn một từ điển có chứa

{ord['!']: None, ord['@']: None, ...}

Thậm chí đơn giản hơn, như một câu trả lời khác đặt ra, hãy tạo bảng dịch tại chỗ:

unicode_line = unicode_line.translate[{ord[c]: None for c in '!@#$'}]

Hoặc, như được đưa lên bởi Joseph Lee, tạo ra cùng một bảng dịch với

translation_table = dict.fromkeys[map[ord, '!@#$'], None]
unicode_line = unicode_line.translate[translation_table]
4:

unicode_line = unicode_line.translate[str.maketrans['', '', '!@#$']]

* Để tương thích với Pythons trước đó, bạn có thể tạo bảng dịch "null" để vượt qua thay cho

import re
line = re.sub['[!@#$]', '', line]
9:

import string
line = line.translate[string.maketrans['', ''], '!@#$']

Ở đây

translation_table = dict.fromkeys[map[ord, '!@#$'], None]
unicode_line = unicode_line.translate[translation_table]
6 được sử dụng để tạo bảng dịch, chỉ là một chuỗi chứa các ký tự có giá trị thứ tự 0 đến 255.

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.

Phương pháp 1: Xóa dấu câu từ chuỗi có dịch

Hai đố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

translation_table = dict.fromkeys[map[ord, '!@#$'], None]
unicode_line = unicode_line.translate[translation_table]
7
translation_table = dict.fromkeys[map[ord, '!@#$'], None]
unicode_line = unicode_line.translate[translation_table]
8

translation_table = dict.fromkeys[map[ord, '!@#$'], None]
unicode_line = unicode_line.translate[translation_table]
9
{ord['!']: None, ord['@']: None, ...}
0
{ord['!']: None, ord['@']: None, ...}
1

translation_table = dict.fromkeys[map[ord, '!@#$'], None]
unicode_line = unicode_line.translate[translation_table]
9
{ord['!']: None, ord['@']: None, ...}
0
{ord['!']: None, ord['@']: None, ...}
4

{ord['!']: None, ord['@']: None, ...}
5
{ord['!']: None, ord['@']: None, ...}
6
{ord['!']: None, ord['@']: None, ...}
7
{ord['!']: None, ord['@']: None, ...}
8
{ord['!']: None, ord['@']: None, ...}
9
unicode_line = unicode_line.translate[{ord[c]: None for c in '!@#$'}]
0

unicode_line = unicode_line.translate[{ord[c]: None for c in '!@#$'}]
1
unicode_line = unicode_line.translate[{ord[c]: None for c in '!@#$'}]
2

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 đó.

Python3

translation_table = dict.fromkeys[map[ord, '!@#$'], None]
unicode_line = unicode_line.translate[translation_table]
9
{ord['!']: None, ord['@']: None, ...}
0
unicode_line = unicode_line.translate[{ord[c]: None for c in '!@#$'}]
5

unicode_line = unicode_line.translate[{ord[c]: None for c in '!@#$'}]
1
{ord['!']: None, ord['@']: None, ...}
6
unicode_line = unicode_line.translate[{ord[c]: None for c in '!@#$'}]
8
unicode_line = unicode_line.translate[{ord[c]: None for c in '!@#$'}]
9
unicode_line = unicode_line.translate[str.maketrans['', '', '!@#$']]
0

unicode_line = unicode_line.translate[str.maketrans['', '', '!@#$']]
1
{ord['!']: None, ord['@']: None, ...}
0

unicode_line = unicode_line.translate[str.maketrans['', '', '!@#$']]
3
unicode_line = unicode_line.translate[str.maketrans['', '', '!@#$']]
4
unicode_line = unicode_line.translate[str.maketrans['', '', '!@#$']]
5
unicode_line = unicode_line.translate[str.maketrans['', '', '!@#$']]
6

{ord['!']: None, ord['@']: None, ...}
5
unicode_line = unicode_line.translate[str.maketrans['', '', '!@#$']]
8
unicode_line = unicode_line.translate[str.maketrans['', '', '!@#$']]
4
unicode_line = unicode_line.translate[str.maketrans['', '', '!@#$']]
5
import string
line = line.translate[string.maketrans['', ''], '!@#$']
1

import string
line = line.translate[string.maketrans['', ''], '!@#$']
2
translation_table = dict.fromkeys[map[ord, '!@#$'], None]
unicode_line = unicode_line.translate[translation_table]
9
{ord['!']: None, ord['@']: None, ...}
0
import string
line = line.translate[string.maketrans['', ''], '!@#$']
5

unicode_line = unicode_line.translate[{ord[c]: None for c in '!@#$'}]
1
{ord['!']: None, ord['@']: None, ...}
6
import string
line = line.translate[string.maketrans['', ''], '!@#$']
8
unicode_line = unicode_line.translate[{ord[c]: None for c in '!@#$'}]
9
unicode_line = unicode_line.translate[str.maketrans['', '', '!@#$']]
0

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

translation_table = dict.fromkeys[map[ord, '!@#$'], None]
unicode_line = unicode_line.translate[translation_table]
7
Gfg is best for  Geeks 
2

translation_table = dict.fromkeys[map[ord, '!@#$'], None]
unicode_line = unicode_line.translate[translation_table]
9
{ord['!']: None, ord['@']: None, ...}
0
unicode_line = unicode_line.translate[{ord[c]: None for c in '!@#$'}]
5

unicode_line = unicode_line.translate[{ord[c]: None for c in '!@#$'}]
1
{ord['!']: None, ord['@']: None, ...}
6
unicode_line = unicode_line.translate[{ord[c]: None for c in '!@#$'}]
8
unicode_line = unicode_line.translate[{ord[c]: None for c in '!@#$'}]
9
unicode_line = unicode_line.translate[str.maketrans['', '', '!@#$']]
0

unicode_line = unicode_line.translate[str.maketrans['', '', '!@#$']]
3
unicode_line = unicode_line.translate[str.maketrans['', '', '!@#$']]
4
unicode_line = unicode_line.translate[str.maketrans['', '', '!@#$']]
5
unicode_line = unicode_line.translate[str.maketrans['', '', '!@#$']]
6

{ord['!']: None, ord['@']: None, ...}
5
unicode_line = unicode_line.translate[str.maketrans['', '', '!@#$']]
8
unicode_line = unicode_line.translate[str.maketrans['', '', '!@#$']]
4
unicode_line = unicode_line.translate[str.maketrans['', '', '!@#$']]
5
import string
line = line.translate[string.maketrans['', ''], '!@#$']
1

import string
line = line.translate[string.maketrans['', ''], '!@#$']
2
translation_table = dict.fromkeys[map[ord, '!@#$'], None]
unicode_line = unicode_line.translate[translation_table]
9
{ord['!']: None, ord['@']: None, ...}
0
import string
line = line.translate[string.maketrans['', ''], '!@#$']
5

The original string is : Gfg, is best : for ! Geeks ;
The string after punctuation filter : Gfg is best  for  Geeks 

unicode_line = unicode_line.translate[{ord[c]: None for c in '!@#$'}]
1
{ord['!']: None, ord['@']: None, ...}
6
import string
line = line.translate[string.maketrans['', ''], '!@#$']
8
unicode_line = unicode_line.translate[{ord[c]: None for c in '!@#$'}]
9
unicode_line = unicode_line.translate[str.maketrans['', '', '!@#$']]
0

Python3

translation_table = dict.fromkeys[map[ord, '!@#$'], None]
unicode_line = unicode_line.translate[translation_table]
9
{ord['!']: None, ord['@']: None, ...}
0
unicode_line = unicode_line.translate[{ord[c]: None for c in '!@#$'}]
5

unicode_line = unicode_line.translate[{ord[c]: None for c in '!@#$'}]
1
{ord['!']: None, ord['@']: None, ...}
6
unicode_line = unicode_line.translate[{ord[c]: None for c in '!@#$'}]
8
unicode_line = unicode_line.translate[{ord[c]: None for c in '!@#$'}]
9
unicode_line = unicode_line.translate[str.maketrans['', '', '!@#$']]
0

unicode_line = unicode_line.translate[str.maketrans['', '', '!@#$']]
1
{ord['!']: None, ord['@']: None, ...}
0

import re
line = re.sub['[!@#$]', '', line]
01
{ord['!']: None, ord['@']: None, ...}
0
import re
line = re.sub['[!@#$]', '', line]
03

unicode_line = unicode_line.translate[str.maketrans['', '', '!@#$']]
3
unicode_line = unicode_line.translate[str.maketrans['', '', '!@#$']]
4
unicode_line = unicode_line.translate[str.maketrans['', '', '!@#$']]
5
unicode_line = unicode_line.translate[str.maketrans['', '', '!@#$']]
6

{ord['!']: None, ord['@']: None, ...}
5
unicode_line = unicode_line.translate[str.maketrans['', '', '!@#$']]
8
unicode_line = unicode_line.translate[str.maketrans['', '', '!@#$']]
4
unicode_line = unicode_line.translate[str.maketrans['', '', '!@#$']]
5
import string
line = line.translate[string.maketrans['', ''], '!@#$']
1

import string
line = line.translate[string.maketrans['', ''], '!@#$']
2
import re
line = re.sub['[!@#$]', '', line]
01
unicode_line = unicode_line.translate[{ord[c]: None for c in '!@#$'}]
9
{ord['!']: None, ord['@']: None, ...}
0
import re
line = re.sub['[!@#$]', '', line]
18

{ord['!']: None, ord['@']: None, ...}
5
unicode_line = unicode_line.translate[str.maketrans['', '', '!@#$']]
8
unicode_line = unicode_line.translate[str.maketrans['', '', '!@#$']]
4
unicode_line = unicode_line.translate[str.maketrans['', '', '!@#$']]
5
import string
line = line.translate[string.maketrans['', ''], '!@#$']
1

import string
line = line.translate[string.maketrans['', ''], '!@#$']
2
translation_table = dict.fromkeys[map[ord, '!@#$'], None]
unicode_line = unicode_line.translate[translation_table]
9
{ord['!']: None, ord['@']: None, ...}
0
import string
line = line.translate[string.maketrans['', ''], '!@#$']
5

import re
line = re.sub['[!@#$]', '', line]
0

unicode_line = unicode_line.translate[{ord[c]: None for c in '!@#$'}]
1
{ord['!']: None, ord['@']: None, ...}
6
import string
line = line.translate[string.maketrans['', ''], '!@#$']
8
unicode_line = unicode_line.translate[{ord[c]: None for c in '!@#$'}]
9
unicode_line = unicode_line.translate[str.maketrans['', '', '!@#$']]
0

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]


Làm thế nào để bạn loại bỏ tất cả các khoảng thời gian từ một chuỗi?

Sử dụng phương thức String.replace [] để xóa tất cả các dấu chấm khỏi chuỗi, ví dụ: const DotsRemove = str. thay thế [/\./ g, '']; . Phương thức thay thế [] sẽ loại bỏ tất cả các dấu chấm khỏi chuỗi bằng cách thay thế chúng bằng các chuỗi trống. replace[] method to remove all dots from a string, e.g. const dotsRemoved = str. replace[/\./g, '']; . The replace[] method will remove all dots from the string by replacing them with empty strings.

Làm thế nào để bạn loại bỏ dấu chấm câu trong Python?

Một trong những phương pháp dễ nhất và nhanh nhất mà qua đó các dấu chấm câu và các ký tự đặc biệt có thể được xóa khỏi chuỗi là bằng cách sử dụng phương thức dịch []. Hàm dịch tích hợp [] có sẵn trong thư viện chuỗi của Python.using the translate [] method. The built-in translate [] function is available in the string library of Python.

Làm cách nào để loại bỏ dấu câu từ DataFrame trong Python?

Làm cách nào để loại bỏ dấu chấm câu từ một cột trong DataFrame trong Python ?..
# Xác định chức năng để xóa dấu câu ..
def remove_punctuations [văn bản]:.
cho dấu câu trong chuỗi.chấm câu:.
Text = Text.thay thế [dấu câu, ''].
Trở về văn bản ..
# Áp dụng cho loạt DF ..
df ['new_column'] = df ['cột'] ..

Làm thế nào để bạn loại bỏ các dấu chấm khỏi một chuỗi trong Python?

Lưu câu trả lời này.Hiển thị hoạt động trên bài viết này.def add_dots [to_add]: res = "" cho chữ cái trong to_add: res = res + chữ cái + "."in {:-1} res] def remove_dots [to_remove]: in [to_remove]: add_dots ["test"] remove_dots ["t.e.s.t."]def remove_dots[to_remove]: print [to_remove]: add_dots["test"] remove_dots["t.e.s.t."]

Bài Viết Liên Quan

Chủ Đề