Hướng dẫn how to replace special characters in python using regex - cách thay thế các ký tự đặc biệt trong python bằng regex

Tôi đang sử dụng urllib để lấy một chuỗi HTML từ một trang web và cần đưa mỗi từ vào tài liệu HTML vào một danh sách.

Đây là mã tôi có cho đến nay. Tôi tiếp tục nhận được một lỗi. Tôi cũng đã sao chép lỗi dưới đây.

import urllib.request

url = input("Please enter a URL: ")

z=urllib.request.urlopen(url)
z=str(z.read())
removeSpecialChars = str.replace("!@#$%^&*()[]{};:,./<>?\|`~-=_+", " ")

words = removeSpecialChars.split()

print ("Words list: ", words[0:20])

Đây là lỗi.

Please enter a URL: http://simleyfootball.com
Traceback (most recent call last):
  File "C:\Users\jeremy.KLUG\My Documents\LiClipse Workspace\Python Project 2\Module2.py", line 7, in 
    removeSpecialChars = str.replace("!@#$%^&*()[]{};:,./<>?\|`~-=_+", " ")
TypeError: replace() takes at least 2 arguments (1 given)

Hướng dẫn how to replace special characters in python using regex - cách thay thế các ký tự đặc biệt trong python bằng regex

Đã hỏi ngày 2 tháng 6 năm 2014 lúc 13:47Jun 2, 2014 at 13:47

Một cách là sử dụng re.sub, đó là cách ưa thích của tôi.

import re
my_str = "hey th~!ere"
my_new_string = re.sub('[^a-zA-Z0-9 \n\.]', '', my_str)
print my_new_string

Output:

hey there

Một cách khác là sử dụng re.escape:

import string
import re

my_str = "hey th~!ere"

chars = re.escape(string.punctuation)
print re.sub(r'['+chars+']', '',my_str)

Output:

hey there

Chỉ là một mẹo nhỏ về kiểu tham số trong Python bởi các tham số PEP-8 nên là

Please enter a URL: http://simleyfootball.com
Traceback (most recent call last):
  File "C:\Users\jeremy.KLUG\My Documents\LiClipse Workspace\Python Project 2\Module2.py", line 7, in 
    removeSpecialChars = str.replace("!@#$%^&*()[]{};:,./<>?\|`~-=_+", " ")
TypeError: replace() takes at least 2 arguments (1 given)
1 chứ không phải
Please enter a URL: http://simleyfootball.com
Traceback (most recent call last):
  File "C:\Users\jeremy.KLUG\My Documents\LiClipse Workspace\Python Project 2\Module2.py", line 7, in 
    removeSpecialChars = str.replace("!@#$%^&*()[]{};:,./<>?\|`~-=_+", " ")
TypeError: replace() takes at least 2 arguments (1 given)
2
about parameters style in python by PEP-8 parameters should be
Please enter a URL: http://simleyfootball.com
Traceback (most recent call last):
  File "C:\Users\jeremy.KLUG\My Documents\LiClipse Workspace\Python Project 2\Module2.py", line 7, in 
    removeSpecialChars = str.replace("!@#$%^&*()[]{};:,./<>?\|`~-=_+", " ")
TypeError: replace() takes at least 2 arguments (1 given)
1 and not
Please enter a URL: http://simleyfootball.com
Traceback (most recent call last):
  File "C:\Users\jeremy.KLUG\My Documents\LiClipse Workspace\Python Project 2\Module2.py", line 7, in 
    removeSpecialChars = str.replace("!@#$%^&*()[]{};:,./<>?\|`~-=_+", " ")
TypeError: replace() takes at least 2 arguments (1 given)
2

Ngoài ra, nếu bạn muốn giữ các khoảng trống chỉ cần thay đổi

Please enter a URL: http://simleyfootball.com
Traceback (most recent call last):
  File "C:\Users\jeremy.KLUG\My Documents\LiClipse Workspace\Python Project 2\Module2.py", line 7, in 
    removeSpecialChars = str.replace("!@#$%^&*()[]{};:,./<>?\|`~-=_+", " ")
TypeError: replace() takes at least 2 arguments (1 given)
3 thành
Please enter a URL: http://simleyfootball.com
Traceback (most recent call last):
  File "C:\Users\jeremy.KLUG\My Documents\LiClipse Workspace\Python Project 2\Module2.py", line 7, in 
    removeSpecialChars = str.replace("!@#$%^&*()[]{};:,./<>?\|`~-=_+", " ")
TypeError: replace() takes at least 2 arguments (1 given)
4keep the spaces just change
Please enter a URL: http://simleyfootball.com
Traceback (most recent call last):
  File "C:\Users\jeremy.KLUG\My Documents\LiClipse Workspace\Python Project 2\Module2.py", line 7, in 
    removeSpecialChars = str.replace("!@#$%^&*()[]{};:,./<>?\|`~-=_+", " ")
TypeError: replace() takes at least 2 arguments (1 given)
3 to
Please enter a URL: http://simleyfootball.com
Traceback (most recent call last):
  File "C:\Users\jeremy.KLUG\My Documents\LiClipse Workspace\Python Project 2\Module2.py", line 7, in 
    removeSpecialChars = str.replace("!@#$%^&*()[]{};:,./<>?\|`~-=_+", " ")
TypeError: replace() takes at least 2 arguments (1 given)
4

Đã trả lời ngày 2 tháng 6 năm 2014 lúc 14:01Jun 2, 2014 at 14:01

Kobi Kkobi kKobi K

7.5276 Huy hiệu vàng39 Huy hiệu bạc84 Huy hiệu đồng6 gold badges39 silver badges84 bronze badges

3

Str.Replace là chức năng sai cho những gì bạn muốn làm (ngoài việc nó được sử dụng không chính xác). Bạn muốn thay thế bất kỳ ký tự nào của một tập hợp bằng một không gian, không phải toàn bộ tập hợp với một không gian duy nhất (cái sau là những gì thay thế). Bạn có thể sử dụng dịch như thế này:

removeSpecialChars = z.translate ({ord(c): " " for c in "!@#$%^&*()[]{};:,./<>?\|`~-=_+"})

Điều này tạo ra một ánh xạ ánh xạ mọi ký tự trong danh sách các ký tự đặc biệt của bạn vào một không gian, sau đó gọi dịch () trên chuỗi, thay thế mọi ký tự trong tập hợp các ký tự đặc biệt bằng một không gian.

Đã trả lời ngày 2 tháng 6 năm 2014 lúc 14:02Jun 2, 2014 at 14:02

Rassahahrassahahrassahah

7265 huy hiệu bạc6 Huy hiệu đồng5 silver badges6 bronze badges

4

Bạn cần gọi

Please enter a URL: http://simleyfootball.com
Traceback (most recent call last):
  File "C:\Users\jeremy.KLUG\My Documents\LiClipse Workspace\Python Project 2\Module2.py", line 7, in 
    removeSpecialChars = str.replace("!@#$%^&*()[]{};:,./<>?\|`~-=_+", " ")
TypeError: replace() takes at least 2 arguments (1 given)
5 trên
Please enter a URL: http://simleyfootball.com
Traceback (most recent call last):
  File "C:\Users\jeremy.KLUG\My Documents\LiClipse Workspace\Python Project 2\Module2.py", line 7, in 
    removeSpecialChars = str.replace("!@#$%^&*()[]{};:,./<>?\|`~-=_+", " ")
TypeError: replace() takes at least 2 arguments (1 given)
6 chứ không phải trên
Please enter a URL: http://simleyfootball.com
Traceback (most recent call last):
  File "C:\Users\jeremy.KLUG\My Documents\LiClipse Workspace\Python Project 2\Module2.py", line 7, in 
    removeSpecialChars = str.replace("!@#$%^&*()[]{};:,./<>?\|`~-=_+", " ")
TypeError: replace() takes at least 2 arguments (1 given)
7, vì bạn muốn thay thế các ký tự nằm trong biến chuỗi
Please enter a URL: http://simleyfootball.com
Traceback (most recent call last):
  File "C:\Users\jeremy.KLUG\My Documents\LiClipse Workspace\Python Project 2\Module2.py", line 7, in 
    removeSpecialChars = str.replace("!@#$%^&*()[]{};:,./<>?\|`~-=_+", " ")
TypeError: replace() takes at least 2 arguments (1 given)
6

removeSpecialChars = z.replace("!@#$%^&*()[]{};:,./<>?\|`~-=_+", " ")

Nhưng điều này sẽ không hoạt động, vì thay thế tìm kiếm một chuỗi con, rất có thể bạn sẽ cần sử dụng mô -đun biểu thức thông thường

Please enter a URL: http://simleyfootball.com
Traceback (most recent call last):
  File "C:\Users\jeremy.KLUG\My Documents\LiClipse Workspace\Python Project 2\Module2.py", line 7, in 
    removeSpecialChars = str.replace("!@#$%^&*()[]{};:,./<>?\|`~-=_+", " ")
TypeError: replace() takes at least 2 arguments (1 given)
9 với hàm
import re
my_str = "hey th~!ere"
my_new_string = re.sub('[^a-zA-Z0-9 \n\.]', '', my_str)
print my_new_string
0:

import re
removeSpecialChars = re.sub("[!@#$%^&*()[]{};:,./<>?\|`~-=_+]", " ", z)

Đừng quên

import re
my_str = "hey th~!ere"
my_new_string = re.sub('[^a-zA-Z0-9 \n\.]', '', my_str)
print my_new_string
1, điều này chỉ ra rằng đây là một tập hợp các ký tự được thay thế.

Đã trả lời ngày 2 tháng 6 năm 2014 lúc 13:58Jun 2, 2014 at 13:58

Danny Mdanny mDanny M

4072 Huy hiệu bạc6 Huy hiệu Đồng2 silver badges6 bronze badges

thay thế hoạt động trên một chuỗi cụ thể, vì vậy bạn cần gọi nó như thế này

removeSpecialChars = z.replace("!@#$%^&*()[]{};:,./<>?\|`~-=_+", " ")

Nhưng đây có lẽ không phải là những gì bạn cần, vì điều này sẽ tìm kiếm một chuỗi duy nhất chứa tất cả các ký tự theo cùng một thứ tự. Bạn có thể làm điều đó với một regexp, như Daniel Michaud đã chỉ ra.

Như một lưu ý phụ, bạn có thể muốn tìm kiếm BeautifulSoup, một thư viện để phân tích cú pháp văn bản được định dạng HTML lộn xộn như những gì bạn thường nhận được từ các trang web.

Đã trả lời ngày 2 tháng 6 năm 2014 lúc 13:51Jun 2, 2014 at 13:51

PavelpavelPavel

7.2862 Huy hiệu vàng28 Huy hiệu bạc42 Huy hiệu đồng2 gold badges28 silver badges42 bronze badges

2

Bạn có thể thay thế các ký tự đặc biệt bằng các ký tự mong muốn như sau,

Please enter a URL: http://simleyfootball.com
Traceback (most recent call last):
  File "C:\Users\jeremy.KLUG\My Documents\LiClipse Workspace\Python Project 2\Module2.py", line 7, in 
    removeSpecialChars = str.replace("!@#$%^&*()[]{};:,./<>?\|`~-=_+", " ")
TypeError: replace() takes at least 2 arguments (1 given)
0

Đã trả lời ngày 12 tháng 2 năm 2015 lúc 16:08Feb 12, 2015 at 16:08

Surendransurendransurendran

4801 Huy hiệu vàng8 Huy hiệu bạc18 Huy hiệu đồng1 gold badge8 silver badges18 bronze badges

Dịch có vẻ nhanh hơn:

N = 100000, 30 ký tự đặc biệt, độ dài chuỗi = 70

Thay thế: 0.3251810073852539 Re.Sub: 0.2859320640563965 Dịch: 0.12320685386657715

Đã trả lời ngày 2 tháng 9 lúc 17:35Sep 2 at 17:35

YanoyanoYano

5985 Huy hiệu bạc8 Huy hiệu đồng5 silver badges8 bronze badges