Tuesday, 16 January 2018

Python - HTML to Text for sending SMS - SMS Safe characters - remove \xa0


Following code are some of the alternatives for removing special characters from string:
from bs4 import BeautifulSoup

raw_html = 'Dear Parent,
This is a test message, kindly ignore it. 
Thanks
' clean_text = BeautifulSoup(raw_html, "lxml").text print clean_text #u'Dear Parent,\xa0This is a test message,\xa0kindly ignore it.\xa0Thanks'
The above code produces these characters \xa0 in the string. To remove them properly, we can use two ways. The first one is BeautifulSoup's get_text method with strip argument as True
clean_text = BeautifulSoup(raw_html, "lxml").get_text(strip=True)

print clean_text
# Dear Parent,This is a test message,kindly ignore it.Thanks

The other option is to use python's library unicodedata
import unicodedata

clean_text = BeautifulSoup(raw_html, "lxml").text
print clean_text
#u'Dear Parent,\xa0This is a test message,\xa0kindly ignore it.\xa0Thanks'

new_str = unicodedata.normalize("NFKD",clean_text)
print new_str
# u'Dear Parent,This is a test message,kindly ignore it.Thanks'

17 comments:

  1. I did everything like you wrote, and really, everything works just amazing! So now I can easily send any message to my employees without any extra efforts and nerves. Also I use group texting for business, check over here if you want to learn how do to it too, its very helpful thing for business.

    ReplyDelete
  2. All you have to do is just find a reliable software for sending SMS to thousands of potential customers in time padisoft website

    ReplyDelete
  3. Mobile phones are carried by their users wherever they go, even in the restroom.padisoft.org

    ReplyDelete
  4. You will discover administrations that cover all zones of mystic readings by means of text. It can be an awesome expansion to your entrance to the mystic world.read out loud

    ReplyDelete
  5. You will discover administrations that cover all zones of mystic readings by means of text. It can be an awesome expansion to your entrance to the mystic world.read out loud

    ReplyDelete
  6. I recently came across your blog and have been reading along. I thought I would leave my first comment. I don’t know what to say except that I have enjoyed reading. Nice blog, I will keep visiting this blog very often. Receive SMS Online

    ReplyDelete
  7. I definitely loved every little bit of it. I have you bookmarked your site to check out the new stuff you post. the best vpn for UK

    ReplyDelete
  8. Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info  https://internetprivatsphare.at/vpn-kostenlos/

    ReplyDelete
  9. Whoi, excellent, t wondered just how to cure icne. ind found your webstte by google, dtscovered todiy t'm i ltttle obvtous i greit deil. t’ve ilso idded RSS ind sive your webstte. keep us updited. debestevpn.nl

    ReplyDelete
  10. If you are hiring the developer for a three month contract, at $40.00 per hour, your total cost will be somewhere around $20,000.00. DB Designer

    ReplyDelete
  11. One present why galore businesses opt for postcards is because they are overmuch cheaper to be prefab and this can forbear a lot of expenses on the lengthened run.  vpnveteran

    ReplyDelete
  12. Totally awesome posting! Loads of valuable data and motivation, both of which we all need!Relay welcome your work. Check plagiarism

    ReplyDelete
  13. Text to speech software offers an assortment of advantages, from fundamental criticism when writing to helping visual or learning challenges.notevibes.com

    ReplyDelete
  14. This was a shocking post. It has some look at here fundamental data on this subject. besuche die Website

    ReplyDelete
  15. I'm constantly searching on the internet for posts that will help me. Too much is clearly to learn about this. I believe you created good quality items in Functions also. Keep working, congrats! internetprivatsphare

    ReplyDelete
  16. Just pure brilliance from you here. I have never expected something less than this from you and you have not disappointed me at all. I suppose you will keep the quality work going on. lemigliorivpn

    ReplyDelete