Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chr for non-ASCII characters in Python

Tags:

python

unicode

I'm trying to implement a search through a list of strings, in a context where there's no way to use something like str.startswith (If you're curious about it, I'm querying the app engine datastore.) I'd like to look for every string that has a certain prefix, let's say 'py'.

I have comparison operators at my disposal, so I was thinking I could implement this as follows.

#pseudo code
search = "py"
search_strings_where(s > search, s < chr(ord(search[0]) + 1)

The chr(ord(search[0]) + 1) is supposed to be the character that's in lexicographical order right after the first character of the search query.

The problem is that this won't work. take for example ord(u"‰"), which returns 8240. But putting that into chragain raises an error.

ValueError: chr() arg not in range(256)

How could I solve this?

EDIT Just found out about unichr, checking if this works. I will write an answer if it does.

like image 369
bigblind Avatar asked Feb 21 '13 16:02

bigblind


People also ask

How to strip out ASCII characters in Python?

Here we can see how to strip out ASCII characters in Python. In this example, we will use the.sub () method in which we have assigned a standard code ‘ [^\x00-\x7f]’ and this code represents the values between 0-127 ASCII code and this method contains the input string ‘new_str’.

How to read non-ASCII characters in Python?

There should be a way to read non-ASCII characters and express them by text in ASCII characters. This approach is related to the inbuilt library unidecode. This library helps Transliterating non-ASCII characters in Python. It provides an u nidecode () method that takes Unicode data and tries to represent it in ASCII.

What is the ASCII value of the character Z in Python?

ASCII value of the character Z is 90. ASCII value of the character a is 97. ASCII value of the character b is 98. ASCII value of the character c is 99. ASCII value of the character z is 122. If we pass a value other than a character to the ord()function, it will raise a TypeError exception.

What is CHR function in Python?

Python chr () Function 1 Definition and Usage. The chr () function returns the character that represents the specified unicode. 2 Syntax 3 Parameter Values. Convert back to unicode with the ord () function.


1 Answers

Perhaps use unichr(), this function will be worked

like image 177
vectorijk Avatar answered Oct 08 '22 22:10

vectorijk