1

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('  ','')
1
  • Perhaps use a translate table to change each of the undesirable chars to the empty string. Commented Jun 11, 2018 at 23:56

2 Answers 2

1

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"
Sign up to request clarification or add additional context in comments.

Comments

0

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 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.