[Answered ]-Javascript / Python Character ASCII Checking

2👍

You get this ‘\xc2\xa3’ with str(“£”) because your Python interpreter converts “£” symbol to UTF-8 (it’s you default locale I think). If you want to get ASCII string, you should do “£”.encode(‘ascii’) but you will get an UnicodeDecodeError exception because it’s not an ASCII character, so you need some filtering.

Even if you make check on client-side with JS the request still can be made straight-forward using curl tool or anything similar, so it would be better to have check on client side and filtering on server side.

On server side you can do something like that:

>>> s = "ascii text 123 £ 456 go go"
>>> t = filter(lambda x: x.isalnum(), s)
>>> print t
"ascii text 123  456 go go"

But this code will filter all non-alphanumeric characters. If you want to allow all printable ASCII chars, try this instead:

>>> import string
>>> string.printable
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'
>>> t = filter(lambda x: x in string.printable, s)
>>> print t
'ascii text 123  456 go go'

0👍

If the library is wrapped in Python code, why not do the check in Python, rather than Javascript? The mistake is one Python wouldn’t let you make. In your if statement:

(asciiNumber = 20)

Should be:

(asciiNumber == 20)

Leave a comment