python - 如何一起或同时使用多个替换,Python

标签 python web-scraping

我有一些文本不清楚,并且有很多标签和 ascii,如下所示,

val=

"\nRated\xa0\n           I have been to this place for dinner tonight.
        \nWell I didn't found anything extraordinary there but indeed a meal worth 
        the price. The number of barbeque item and other both were good.\n\nFood: 3.5/5\"

因此,为了清楚地表明我正在使用这个标签

  val.text.replace('\t', '').replace('\n', '').encode('ascii','ignore').
decode("utf-8").replace('Rated','').replace('  ','')

并使用多次替换,我得到了我的o/p -

I have been to this place for dinner tonight. Well I didn't found anything extraordinary there but indeed a meal worth the price. The number of barbeque item and other both were good. Food: 3.5/5

我想知道有什么办法可以让我一次只使用替换来进行类似的替换。就像在这种情况下 -

replace('\t', '').replace('\n', '').replace('  ','')

最佳答案

您可以使用 .translate 删除 \n\t,然后使用替换的空格:

>>> val.translate(None,'\n\t').replace('  ','')
"Rated I have been to this place for dinner tonight.Well I didn't found anything extraordinary there but indeed a meal worth the price. The number of barbeque item and other both were good.Food: 3.5/5"

replace(' ','') 在运行偶数空格时会出现问题(它们将被删除)。您可以考虑使用正则表达式:

>>> re.sub(r'(\b  *\b)',' ',val.translate(None,'\n\t'))
"Rated I have been to this place for dinner tonight.Well I didn't found anything extraordinary there but indeed a meal worth the price. The number of barbeque item and other both were good.Food: 3.5/5"

关于python - 如何一起或同时使用多个替换,Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50807518/

相关文章:

python - 为什么 psycopg2 不执行我的任何 SQL 函数? (索引错误 : tuple index out of range)

python - 帮助安装 cx_Oracle

python - scrapy 分页 Selenium python

javascript - 使用 Json 和 BS4 抓取 HTML 中的脚本标签

clojure - 使用 enlive 解析 html 片段

python - 使用 Fiddler 使用 python 脚本复制浏览器操作

python - openpyxl如何在工作表上编辑输入数据后读取公式结果? data_only=True 给我一个 "None"结果

python - 如何在 pyqt 中同时运行 2 个线程?

Python 的 BeautifulSoup 作为 Web 应用程序?

python - 使用 scrapy 爬取时的动态起始 url 列表