我有一些文本不清楚,并且有很多标签和 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/