Hướng dẫn how do i remove symbols from text in python? - làm cách nào để xóa các ký hiệu khỏi văn bản trong python?

103

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Tôi là người mới bắt đầu với cả Python và Regex, và tôi muốn biết cách tạo một chuỗi lấy các biểu tượng và thay thế chúng bằng khoảng trắng. Bất kỳ sự giúp đỡ là tuyệt vời.

Ví dụ:

how much for the maple syrup? $20.99? That's ricidulous!!!

into:

how much for the maple syrup 20 99 That s ridiculous

Khi được hỏi ngày 18 tháng 5 năm 2009 lúc 1:55May 18, 2009 at 1:55

Hướng dẫn how do i remove symbols from text in python? - làm cách nào để xóa các ký hiệu khỏi văn bản trong python?

2

Một cách, sử dụng các biểu thức thông thường:

>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
  • how much for the maple syrup 20 99 That s ridiculous
    
    4 sẽ phù hợp với các ký tự chữ và số và nhấn mạnh

  • how much for the maple syrup 20 99 That s ridiculous
    
    5 sẽ phù hợp với bất cứ thứ gì không phải là chữ và số

Hướng dẫn how do i remove symbols from text in python? - làm cách nào để xóa các ký hiệu khỏi văn bản trong python?

Đá xanh

7.7486 Huy hiệu vàng31 Huy hiệu bạc51 Huy hiệu Đồng6 gold badges31 silver badges51 bronze badges

Đã trả lời ngày 18 tháng 5 năm 2009 lúc 1:59May 18, 2009 at 1:59

dF.dF.dF.

72.4K29 Huy hiệu vàng128 Huy hiệu bạc135 Huy hiệu đồng29 gold badges128 silver badges135 bronze badges

5

Đôi khi phải mất nhiều thời gian hơn để tìm ra regex hơn là chỉ viết nó ra trong Python:

import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')

Nếu bạn cần các ký tự khác, bạn có thể thay đổi nó để sử dụng danh sách trắng hoặc mở rộng danh sách đen của bạn.

Danh sách trắng mẫu:

whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '

Danh sách trắng mẫu bằng cách sử dụng biểu thức máy phát:

whitelist = string.letters + string.digits + ' '
new_s = ''.join(c for c in s if c in whitelist)

Đã trả lời ngày 18 tháng 5 năm 2009 lúc 9:24May 18, 2009 at 9:24

Hướng dẫn how do i remove symbols from text in python? - làm cách nào để xóa các ký hiệu khỏi văn bản trong python?

Monkutmonkutmonkut

40.4K23 Huy hiệu vàng120 Huy hiệu bạc148 Huy hiệu đồng23 gold badges120 silver badges148 bronze badges

2

Tôi thường chỉ mở bảng điều khiển và tìm kiếm giải pháp trong các phương thức đối tượng. Thường thì nó đã ở đó:

>>> a = "hello ' s"
>>> dir(a)
[ (....) 'partition', 'replace' (....)]
>>> a.replace("'", " ")
'hello   s'

Câu trả lời ngắn gọn: Sử dụng

how much for the maple syrup 20 99 That s ridiculous
6.

Nathan

3.7392 Huy hiệu vàng19 Huy hiệu bạc21 Huy hiệu đồng2 gold badges19 silver badges21 bronze badges

Đã trả lời ngày 18 tháng 5 năm 2009 lúc 5:45May 18, 2009 at 5:45

Busterbusterbuster

Huy hiệu vàng 95111 gold badge8 silver badges15 bronze badges

1

Vấn đề chung mà các lập trình viên phải đối mặt là loại bỏ các ký tự không mong muốn khỏi một chuỗi bằng Python. Nhưng đôi khi yêu cầu ở trên và yêu cầu loại bỏ hơn 1 nhân vật, nhưng một danh sách các nhân vật độc hại như vậy. Đây có thể ở dạng ký tự đặc biệt để xây dựng lại mật khẩu hợp lệ và nhiều ứng dụng khác có thể. Vì vậy, nhiệm vụ của chúng tôi là loại bỏ các ký tự không mong muốn khỏi chuỗi.

Xóa ký hiệu khỏi chuỗi bằng str.alsalnum ()

Phương thức Python String isalnum () kiểm tra xem tất cả các ký tự trong một chuỗi nhất định có phải là chữ và số hay không. Nó trả về một boolean là đúng - nếu tất cả các ký tự là chữ và khác hoặc sai - nếu một hoặc nhiều ký tự không phải là chữ và số.

Python3

how much for the maple syrup 20 99 That s ridiculous
7
how much for the maple syrup 20 99 That s ridiculous
8
how much for the maple syrup 20 99 That s ridiculous
9

>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
0
how much for the maple syrup 20 99 That s ridiculous
8
>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
22____23
>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
4
>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
5

>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
9
import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
0

Output:

GeeksforGeeks

Loại bỏ ký hiệu khỏi chuỗi bằng cách sử dụng thay thế () & nbsp;

Người ta có thể sử dụng str.replace () bên trong một vòng lặp để kiểm tra BAD_CHAR và sau đó thay thế nó bằng chuỗi trống do đó loại bỏ nó. Đây là cách tiếp cận cơ bản nhất và không hiệu quả trên quan điểm hiệu suất.

Python3

import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
1
how much for the maple syrup 20 99 That s ridiculous
8
import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
3
import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
4
import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
5
import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
6
import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
5
import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
8
import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
5____40
import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
5
whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '
2

whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '
4
how much for the maple syrup 20 99 That s ridiculous
8
how much for the maple syrup 20 99 That s ridiculous
9

>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
9
whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '
8
whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '
9
whitelist = string.letters + string.digits + ' '
new_s = ''.join(c for c in s if c in whitelist)
0
whitelist = string.letters + string.digits + ' '
new_s = ''.join(c for c in s if c in whitelist)
1

>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
3
whitelist = string.letters + string.digits + ' '
new_s = ''.join(c for c in s if c in whitelist)
3
>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
5
whitelist = string.letters + string.digits + ' '
new_s = ''.join(c for c in s if c in whitelist)
5

whitelist = string.letters + string.digits + ' '
new_s = ''.join(c for c in s if c in whitelist)
6
whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '
4
how much for the maple syrup 20 99 That s ridiculous
8
whitelist = string.letters + string.digits + ' '
new_s = ''.join(c for c in s if c in whitelist)
9

>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
9
whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '
8
>>> a = "hello ' s"
>>> dir(a)
[ (....) 'partition', 'replace' (....)]
>>> a.replace("'", " ")
'hello   s'
2
whitelist = string.letters + string.digits + ' '
new_s = ''.join(c for c in s if c in whitelist)
0
>>> a = "hello ' s"
>>> dir(a)
[ (....) 'partition', 'replace' (....)]
>>> a.replace("'", " ")
'hello   s'
4
>>> a = "hello ' s"
>>> dir(a)
[ (....) 'partition', 'replace' (....)]
>>> a.replace("'", " ")
'hello   s'
5

Đầu ra: & nbsp; 

Original String : Ge;ek*s:fo!r;Ge*e*k:s!
Resultant list is : GeeksforGeeks

Xóa ký hiệu khỏi chuỗi bằng cách sử dụng Jop () + Generator & nbsp; 

Bằng cách sử dụng python tham gia (), chúng tôi làm lại chuỗi. Trong hàm Trình tạo, chúng tôi chỉ định logic để bỏ qua các ký tự trong bad_chars và do đó xây dựng một chuỗi mới không có ký tự xấu.

Python3

import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
1
how much for the maple syrup 20 99 That s ridiculous
8
import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
3
import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
4
import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
5
import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
6
import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
5
import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
8
import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
5____40
import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
5
whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '
2

whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '
4
how much for the maple syrup 20 99 That s ridiculous
8
how much for the maple syrup 20 99 That s ridiculous
9

>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
9
whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '
8
whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '
9
whitelist = string.letters + string.digits + ' '
new_s = ''.join(c for c in s if c in whitelist)
0
whitelist = string.letters + string.digits + ' '
new_s = ''.join(c for c in s if c in whitelist)
1

>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
3
whitelist = string.letters + string.digits + ' '
new_s = ''.join(c for c in s if c in whitelist)
3
>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
5
whitelist = string.letters + string.digits + ' '
new_s = ''.join(c for c in s if c in whitelist)
5

>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
9
whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '
8
>>> a = "hello ' s"
>>> dir(a)
[ (....) 'partition', 'replace' (....)]
>>> a.replace("'", " ")
'hello   s'
2
whitelist = string.letters + string.digits + ' '
new_s = ''.join(c for c in s if c in whitelist)
0
>>> a = "hello ' s"
>>> dir(a)
[ (....) 'partition', 'replace' (....)]
>>> a.replace("'", " ")
'hello   s'
4
>>> a = "hello ' s"
>>> dir(a)
[ (....) 'partition', 'replace' (....)]
>>> a.replace("'", " ")
'hello   s'
5

Đầu ra: & nbsp; 

Original String : Ge;ek*s:fo!r;Ge*e*k:s!
Resultant list is : GeeksforGeeks

Xóa ký hiệu khỏi chuỗi bằng cách sử dụng Jop () + Generator & nbsp;translate() 

Bằng cách sử dụng python tham gia (), chúng tôi làm lại chuỗi. Trong hàm Trình tạo, chúng tôi chỉ định logic để bỏ qua các ký tự trong bad_chars và do đó xây dựng một chuỗi mới không có ký tự xấu.

Python3

whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '
4
how much for the maple syrup 20 99 That s ridiculous
8
Original String : Ge;ek*s:fo!r;Ge*e*k:s!
Resultant list is : GeeksforGeeks
9
>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
3
whitelist = string.letters + string.digits + ' '
new_s = ''.join(c for c in s if c in whitelist)
3
>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
5

Xóa ký hiệu khỏi chuỗi bằng cách sử dụng dịch () & nbsp;

whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '
4
how much for the maple syrup 20 99 That s ridiculous
8
how much for the maple syrup 20 99 That s ridiculous
9

>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
9
whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '
8
whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '
9
whitelist = string.letters + string.digits + ' '
new_s = ''.join(c for c in s if c in whitelist)
0
whitelist = string.letters + string.digits + ' '
new_s = ''.join(c for c in s if c in whitelist)
1

>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
3
whitelist = string.letters + string.digits + ' '
new_s = ''.join(c for c in s if c in whitelist)
3
>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
5
whitelist = string.letters + string.digits + ' '
new_s = ''.join(c for c in s if c in whitelist)
5

whitelist = string.letters + string.digits + ' '
new_s = ''.join(c for c in s if c in whitelist)
6
whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '
4
how much for the maple syrup 20 99 That s ridiculous
8
whitelist = string.letters + string.digits + ' '
new_s = ''.join(c for c in s if c in whitelist)
9

>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
9
whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '
8
>>> a = "hello ' s"
>>> dir(a)
[ (....) 'partition', 'replace' (....)]
>>> a.replace("'", " ")
'hello   s'
2
whitelist = string.letters + string.digits + ' '
new_s = ''.join(c for c in s if c in whitelist)
0
>>> a = "hello ' s"
>>> dir(a)
[ (....) 'partition', 'replace' (....)]
>>> a.replace("'", " ")
'hello   s'
4
>>> a = "hello ' s"
>>> dir(a)
[ (....) 'partition', 'replace' (....)]
>>> a.replace("'", " ")
'hello   s'
5

whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '
4
how much for the maple syrup 20 99 That s ridiculous
8
how much for the maple syrup 20 99 That s ridiculous
44

>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
9
whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '
8
>>> a = "hello ' s"
>>> dir(a)
[ (....) 'partition', 'replace' (....)]
>>> a.replace("'", " ")
'hello   s'
2
whitelist = string.letters + string.digits + ' '
new_s = ''.join(c for c in s if c in whitelist)
0
>>> a = "hello ' s"
>>> dir(a)
[ (....) 'partition', 'replace' (....)]
>>> a.replace("'", " ")
'hello   s'
4
>>> a = "hello ' s"
>>> dir(a)
[ (....) 'partition', 'replace' (....)]
>>> a.replace("'", " ")
'hello   s'
5

Đầu ra: & nbsp; 

________số 8

Xóa ký hiệu khỏi chuỗi bằng Filter () & NBSP; filter() 

Đây là một giải pháp khác để thực hiện nhiệm vụ này. Sử dụng chức năng Lambda, chức năng bộ lọc có thể xóa tất cả các bad_chars và trả về chuỗi tinh chế mong muốn.

Python3

import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
1
how much for the maple syrup 20 99 That s ridiculous
8
import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
3
import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
4
import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
5
import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
6
import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
5
import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
8
import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
5
whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '
0
whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '
3

whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '
4
how much for the maple syrup 20 99 That s ridiculous
8
how much for the maple syrup 20 99 That s ridiculous
64

>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
9
whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '
8
whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '
9
whitelist = string.letters + string.digits + ' '
new_s = ''.join(c for c in s if c in whitelist)
0
whitelist = string.letters + string.digits + ' '
new_s = ''.join(c for c in s if c in whitelist)
1

whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '
4
how much for the maple syrup 20 99 That s ridiculous
8
how much for the maple syrup 20 99 That s ridiculous
72
how much for the maple syrup 20 99 That s ridiculous
73__

how much for the maple syrup 20 99 That s ridiculous
80
how much for the maple syrup 20 99 That s ridiculous
81

>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
9
whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '
8
>>> a = "hello ' s"
>>> dir(a)
[ (....) 'partition', 'replace' (....)]
>>> a.replace("'", " ")
'hello   s'
2
whitelist = string.letters + string.digits + ' '
new_s = ''.join(c for c in s if c in whitelist)
0
>>> a = "hello ' s"
>>> dir(a)
[ (....) 'partition', 'replace' (....)]
>>> a.replace("'", " ")
'hello   s'
4
>>> a = "hello ' s"
>>> dir(a)
[ (....) 'partition', 'replace' (....)]
>>> a.replace("'", " ")
'hello   s'
5

Output:  

Original String : Ge;ek*s:fo!r;Ge*e*k:s!
Resultant list is : GeeksforGeeks

Đầu ra: & nbsp;re.sub() function: 

________số 8

Python3

Xóa ký hiệu khỏi chuỗi bằng Filter () & NBSP;

Đây là một giải pháp khác để thực hiện nhiệm vụ này. Sử dụng chức năng Lambda, chức năng bộ lọc có thể xóa tất cả các bad_chars và trả về chuỗi tinh chế mong muốn.

import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
1
how much for the maple syrup 20 99 That s ridiculous
8
import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
3
import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
4
import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
5
import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
6
import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
5
import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
8
import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
5
whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '
0
whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '
3

whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '
4
how much for the maple syrup 20 99 That s ridiculous
8
how much for the maple syrup 20 99 That s ridiculous
64

>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
9
whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '
8
whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '
9
whitelist = string.letters + string.digits + ' '
new_s = ''.join(c for c in s if c in whitelist)
0
whitelist = string.letters + string.digits + ' '
new_s = ''.join(c for c in s if c in whitelist)
1

whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '
4
how much for the maple syrup 20 99 That s ridiculous
8
how much for the maple syrup 20 99 That s ridiculous
72
how much for the maple syrup 20 99 That s ridiculous
73__

Xóa ký hiệu khỏi chuỗi bằng hàm re.sub (): & nbsp;

Các biểu thức chính quy được sử dụng để xác định ký tự xấu trong hàm chuỗi và re.sub được sử dụng để thay thế bad_char từ chuỗi. & Nbsp;

how much for the maple syrup 20 99 That s ridiculous
05
how much for the maple syrup 20 99 That s ridiculous
89

Output:  

how much for the maple syrup 20 99 That s ridiculous
2

>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!" >>> re.sub(r'[^\w]', ' ', s) 'how much for the maple syrup 20 99 That s ridiculous ' 0how much for the maple syrup 20 99 That s ridiculous 8 how much for the maple syrup 20 99 That s ridiculous 9

how much for the maple syrup 20 99 That s ridiculous
93
how much for the maple syrup 20 99 That s ridiculous
8
import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
3
how much for the maple syrup 20 99 That s ridiculous
96
import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
5
how much for the maple syrup 20 99 That s ridiculous
98
import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
5
whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '
0
import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
    s = s.replace(char, ' ')
5
>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
02

Python3

>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
9
whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '
8
>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
08
whitelist = string.letters + string.digits + ' '
new_s = ''.join(c for c in s if c in whitelist)
0
>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
10

>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
11
how much for the maple syrup 20 99 That s ridiculous
8
how much for the maple syrup 20 99 That s ridiculous
37

>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
3
whitelist = string.letters + string.digits + ' '
new_s = ''.join(c for c in s if c in whitelist)
3
>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
5
>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
17

>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
55
how much for the maple syrup 20 99 That s ridiculous
8
>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
57

whitelist = string.letters + string.digits + ' '
new_s = ''.join(c for c in s if c in whitelist)
6
>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
11
whitelist = string.letters + string.digits + ' '
new_s = ''.join(c for c in s if c in whitelist)
0
how much for the maple syrup 20 99 That s ridiculous
8
>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
22

>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
23
how much for the maple syrup 20 99 That s ridiculous
8
>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
25
>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
26
>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
27

>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
68
>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
55
whitelist = string.letters + string.digits + ' '
new_s = ''.join(c for c in s if c in whitelist)
0
how much for the maple syrup 20 99 That s ridiculous
8
>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
22

>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
9
whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
    if char in whitelist:
        new_s += char
    else:
        new_s += ' '
8
>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
30
whitelist = string.letters + string.digits + ' '
new_s = ''.join(c for c in s if c in whitelist)
0
>>> a = "hello ' s"
>>> dir(a)
[ (....) 'partition', 'replace' (....)]
>>> a.replace("'", " ")
'hello   s'
4
>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup   20 99  That s ridiculous   '
33

Xóa ký hiệu khỏi chuỗi bằng cách sử dụng trong, không phải trong các toán tử

how much for the maple syrup 20 99 That s ridiculous
3


Làm cách nào để loại bỏ các ký tự đặc biệt khỏi một văn bản trong Python?

Sử dụng 'str. Thay thế (), chúng ta có thể thay thế một ký tự cụ thể. Nếu chúng ta muốn xóa ký tự cụ thể đó, hãy thay thế ký tự đó bằng một chuỗi trống. STR. Phương thức thay thế () sẽ thay thế tất cả các lần xuất hiện của ký tự cụ thể được đề cập.replace that character with an empty string. The str. replace() method will replace all occurrences of the specific character mentioned.

Làm cách nào để loại bỏ một ký hiệu khỏi một chuỗi trong Python?

Trong Python, bạn có thể sử dụng các phương thức thay thế () và dịch () để chỉ định ký tự nào bạn muốn xóa khỏi chuỗi và trả về kết quả chuỗi được sửa đổi mới.Điều quan trọng cần nhớ là chuỗi ban đầu sẽ không bị thay đổi vì chuỗi là bất biến.use the replace() and translate() methods to specify which characters you want to remove from the string and return a new modified string result. It is important to remember that the original string will not be altered because strings are immutable.

Làm cách nào để loại bỏ các biểu tượng không mong muốn trong Python?

Python Xóa ký tự khỏi chuỗi bằng cách sử dụng dịch python python dịch () Chức năng Thay thế từng ký tự trong chuỗi bằng bảng dịch đã cho.Chúng tôi phải chỉ định điểm mã Unicode cho ký tự và 'Không' làm thay thế để xóa nó khỏi chuỗi kết quả.using translate() Python string translate() function replace each character in the string using the given translation table. We have to specify the Unicode code point for the character and 'None' as a replacement to remove it from the result string.

Làm cách nào để loại bỏ các ký tự đặc biệt khỏi dữ liệu trong Python?

Loại bỏ các ký tự đặc biệt bao gồm các chuỗi sử dụng python isalnum.Python có một phương thức chuỗi đặc biệt,.isalnum (), trả về true nếu chuỗi là một ký tự alpha-numeric và trả về sai nếu không.Chúng ta có thể sử dụng điều này, để lặp qua một chuỗi và nối vào một chuỗi mới, chỉ có các ký tự alpha-numeric.Using Python isalnum. Python has a special string method, . isalnum() , which returns True if the string is an alpha-numeric character, and returns False if it is not. We can use this, to loop over a string and append, to a new string, only alpha-numeric characters.