Rahul
Rahul

Reputation: 3386

How to know if a data contains non-ascii character?

I'm working with an api which returns some data in the form of 01234⇒56789. Sometimes this data has only numbers which is not a problem but sometimes it returns character. As I have to automate the filtering process of selecting the number after the arrow (non-ascii character) I have to know when the characters contains a non-ascii character.

I used decode(utf-8) and it returns u'01234\u21d256789' . I tried split('\u21d2') but the string is not splitting. Any help is appreciated.

Upvotes: 0

Views: 753

Answers (1)

user3159253
user3159253

Reputation: 17455

python3:

>>> s = "01234⇒56789"
>>> s
'01234⇒56789'
>>> s.split("⇒")
['01234', '56789']

python2:

>>> s = u"01234⇒56789"
>>> s.split(u"⇒")
[u'01234', u'56789']

the key point in Python2 is to specify that you deal with an unicode string. In Python3 strings are unicode by default and there's bytes type

Upvotes: 1

Related Questions