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
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 '
4 sẽ phù hợp với các ký tự chữ và số và nhấn mạnhhow 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ốhow much for the maple syrup 20 99 That s ridiculous
Đá 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
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
7how 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 '
0how 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 '
9import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
s = s.replace[char, ' ']
0Output:
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, ' ']
1how 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, ' ']
3import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
s = s.replace[char, ' ']
4import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
s = s.replace[char, ' ']
5import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
s = s.replace[char, ' ']
6import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
s = s.replace[char, ' ']
5import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
s = s.replace[char, ' ']
8import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
s = s.replace[char, ' ']
5____40import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
s = s.replace[char, ' ']
5whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
if char in whitelist:
new_s += char
else:
new_s += ' '
2whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
if char in whitelist:
new_s += char
else:
new_s += ' '
4how 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 '
9whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
if char in whitelist:
new_s += char
else:
new_s += ' '
8whitelist = 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]
5whitelist = string.letters + string.digits + ' '
new_s = ''.join[c for c in s if c in whitelist]
6whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
if char in whitelist:
new_s += char
else:
new_s += ' '
4how 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 '
9whitelist = 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, ' ']
1how 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, ' ']
3import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
s = s.replace[char, ' ']
4import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
s = s.replace[char, ' ']
5import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
s = s.replace[char, ' ']
6import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
s = s.replace[char, ' ']
5import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
s = s.replace[char, ' ']
8import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
s = s.replace[char, ' ']
5____40import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
s = s.replace[char, ' ']
5whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
if char in whitelist:
new_s += char
else:
new_s += ' '
2whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
if char in whitelist:
new_s += char
else:
new_s += ' '
4how 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 '
9whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
if char in whitelist:
new_s += char
else:
new_s += ' '
8whitelist = 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 '
9whitelist = 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 += ' '
4how 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 : GeeksforGeeks9
>>> 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 '
5Xó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 += ' '
4how 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 '
9whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
if char in whitelist:
new_s += char
else:
new_s += ' '
8whitelist = 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]
5whitelist = string.letters + string.digits + ' '
new_s = ''.join[c for c in s if c in whitelist]
6whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
if char in whitelist:
new_s += char
else:
new_s += ' '
4how 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 '
9whitelist = 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'
5whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
if char in whitelist:
new_s += char
else:
new_s += ' '
4how 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 '
9whitelist = 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ố 8Xó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, ' ']
1how 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, ' ']
3import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
s = s.replace[char, ' ']
4import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
s = s.replace[char, ' ']
5import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
s = s.replace[char, ' ']
6import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
s = s.replace[char, ' ']
5import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
s = s.replace[char, ' ']
8import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
s = s.replace[char, ' ']
5whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
if char in whitelist:
new_s += char
else:
new_s += ' '
0whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
if char in whitelist:
new_s += char
else:
new_s += ' '
3whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
if char in whitelist:
new_s += char
else:
new_s += ' '
4how 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 '
9whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
if char in whitelist:
new_s += char
else:
new_s += ' '
8whitelist = 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]
1whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
if char in whitelist:
new_s += char
else:
new_s += ' '
4how much for the maple syrup 20 99 That s ridiculous
8 how much for the maple syrup 20 99 That s ridiculous
72how much for the maple syrup 20 99 That s ridiculous
73__how much for the maple syrup 20 99 That s ridiculous
80how 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 '
9whitelist = 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'
5Output:
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, ' ']
1how 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, ' ']
3import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
s = s.replace[char, ' ']
4import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
s = s.replace[char, ' ']
5import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
s = s.replace[char, ' ']
6import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
s = s.replace[char, ' ']
5import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
s = s.replace[char, ' ']
8import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
s = s.replace[char, ' ']
5whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
if char in whitelist:
new_s += char
else:
new_s += ' '
0whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
if char in whitelist:
new_s += char
else:
new_s += ' '
3whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
if char in whitelist:
new_s += char
else:
new_s += ' '
4how 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 '
9whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
if char in whitelist:
new_s += char
else:
new_s += ' '
8whitelist = 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]
1whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
if char in whitelist:
new_s += char
else:
new_s += ' '
4how much for the maple syrup 20 99 That s ridiculous
8 how much for the maple syrup 20 99 That s ridiculous
72how 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
89Output:
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
>>> 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
how much for the maple syrup 20 99 That s ridiculous
how much for the maple syrup 20 99 That s ridiculous
93how 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, ' ']
3how much for the maple syrup 20 99 That s ridiculous
96import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
s = s.replace[char, ' ']
5how much for the maple syrup 20 99 That s ridiculous
98import string
s = "how much for the maple syrup? $20.99? That's ricidulous!!!"
for char in string.punctuation:
s = s.replace[char, ' ']
5whitelist = string.letters + string.digits + ' '
new_s = ''
for char in s:
if char in whitelist:
new_s += char
else:
new_s += ' '
0import 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 '
02Python3
>>> 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 '
9whitelist = 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 '
11how 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 '
55how 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 '
57whitelist = 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 '
11whitelist = string.letters + string.digits + ' '
new_s = ''.join[c for c in s if c in whitelist]
0how 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 '
23how 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 '
55whitelist = string.letters + string.digits + ' '
new_s = ''.join[c for c in s if c in whitelist]
0how 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 '
9whitelist = 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 '
33Xó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