App Engine ndb StringProperty和字符串哈希 [英] App Engine ndb StringProperty and string hashes
问题描述
我使用PyCrypto来生成安全密钥哈希值。我想存储一个或多个我生成的部分密钥。每个部分键的形式为:
pre $ \x0f | 4\xcc\x02b\xc3\xf8\ xb0\xd8\xfc\xd4\x90VE\xf2
我有一个ndb StringProperty()
其中我会存储该信息。然而,它引发了一个 BadValueError
,表示它期望一个UTF-8编码的字符串。我试过使用str的 .encode('uft-8')
方法,但是这也引发了一个错误,告诉我它不能编码,因为定位不好。
无论如何,我的问题是,如何将该字节字符串转换为我可以在ndb中存储的内容?
改进的答案:
在这种情况下,不应将键存储为字符串或文本,而应使用 BlobProperty
,它存储未解释的字节字符串。
原始答案:
要将字节(字符串)转换为Unicode,您可以使用方法 decode
。您还需要使用保留原始二进制数据的编码,即ISO-8859-1。请参阅 ISO-8859-1编码和二进制数据保存
unicode_key = key.decode('iso-8859-1')
bytes_key = unicode_key.encode('iso- 8859-1')
考虑同样使用TextProperty,因为StringProperties被编入索引。
I'm using PyCrypto for generating secure key hashes. I want to store one or more of the partial keys I generate. Each partial key is in the form
\x0f|4\xcc\x02b\xc3\xf8\xb0\xd8\xfc\xd4\x90VE\xf2
I have an ndb StringProperty()
in which I'd lke to store that info. However, it raises a BadValueError
saying it expects an UTF-8 encoded string. I tried using str's .encode('uft-8')
method but that also raises an error telling me it couldn't encode because bad positioning.
Anyway, my question is, how can I convert that byte string into something I can store in ndb?
Improved Answer:
In this case instead of storing the key as String or Text, you should use a BlobProperty
which stores an uninterpreted byte string.
Original Answer:
To convert bytes (strings) to unicode you use the method decode
. You also need to use an encoding that preserves the original binary data, which is ISO-8859-1. See ISO-8859-1 encoding and binary data preservation
unicode_key = key.decode('iso-8859-1')
bytes_key = unicode_key.encode('iso-8859-1')
Consider also using A TextProperty instead, as StringProperties are indexed.
这篇关于App Engine ndb StringProperty和字符串哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!