Reputation: 402
I have some text which is not clear and have so many tags and ascii as follow,
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\"
So for making clear this tag I am using
val.text.replace('\t', '').replace('\n', '').encode('ascii','ignore').
decode("utf-8").replace('Rated','').replace(' ','')
and using multiple times replace I got my o/p as -
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
I want to know that is there any way so I can use replace at once only for similar kind of replacement. like in this case -
replace('\t', '').replace('\n', '').replace(' ','')
Upvotes: 1
Views: 76
Reputation: 103959
You can use .translate
to delete \n\t
and then use your replacement for the runs of spaces:
>>> 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"
The replace(' ','')
will be problematic with runs of even spaces (they will just be deleted). You might consider a regex:
>>> 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"
Upvotes: 1
Reputation: 71590
Well even do i am not using replace
, but i still think this is the best way:
import string
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\"""
"""
print(''.join([i for i in ' '.join(val.split()) if i in string.ascii_letters+' ']))
Output:
Rated I have been to this place for dinner tonight Well I didnt found anything extraordinary there but indeed a meal worth the price The number of barbeque item and other both were good Food
Upvotes: 0