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 chr
again 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.
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’.
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.
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.
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.
Perhaps use unichr(), this function will be worked
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With