In many python applications, we have to process ascii python string, which means we should detect a python string contains non-ascii characters or not. In this tutorial, we will tell you how to do.
Before we start, you should know the scope of ascii characters.
Create a python string which contains non-ascii characters
str = u'这个是我的blog网站:https://www.tutorialexample.com'
Create a regular regression to detect
regexp = re.compile(r'[^\x00-\x7f]')
Notice: here we only process basic ascii characters, which are 0- 127.
If you want to process all ascii characters, which are 0 – 255, you can do like this:
regexp = re.compile(r'[^\x00-\xff]')
Meanwhile, you also can select to process the scope of ascii characters by ascii table.
Detect python string contains non-ascii or not
if regexp.search(str): print('non-ascill charracters are found') else: print("all characters is ascii")
non-ascill charracters are found
Notice: if you have find Non-UTF-8 error, you can refer to this tutorial.