替换字符串python中的特殊字符 [英] replace special characters in a string python

查看:122
本文介绍了替换字符串python中的特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用urllib从网站获取html字符串,并且需要将html文档中的每个单词放入列表中.

I am using urllib to get a string of html from a website and need to put each word in the html document into a list.

这是我到目前为止的代码.我不断收到错误消息.我还复制了以下错误.

Here is the code I have so far. I keep getting an error. I have also copied the error below.

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])

这是错误.

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 <module>
    removeSpecialChars = str.replace("!@#$%^&*()[]{};:,./<>?\|`~-=_+", " ")
TypeError: replace() takes at least 2 arguments (1 given)

推荐答案

str.replace是您要执行的操作错误的函数(除了使用不正确之外).您要用空格替换集合的任何字符,而不是用单个空格替换整个集合(后者是replace的作用).您可以像这样使用翻译:

str.replace is the wrong function for what you want to do (apart from it being used incorrectly). You want to replace any character of a set with a space, not the whole set with a single space (the latter is what replace does). You can use translate like this:

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

这将创建一个映射,该映射将特殊字符列表中的每个字符映射到一个空格,然后在字符串上调用translate(),将特殊字符集中的每个单个字符替换为一个空格.

This creates a mapping which maps every character in your list of special characters to a space, then calls translate() on the string, replacing every single character in the set of special characters with a space.

这篇关于替换字符串python中的特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆