简体   繁体   English

使用 Python3x 的 Google Cloud Datastore 中的 UTF-8 字符串?

[英]UTF-8 strings in the Google Cloud Datastore using Python3x?

I am using Pub/sub, Cloud functions and datastore altogether.我正在使用 Pub/sub、云功能和数据存储。 User sends the data in JSON Format through Pub/Sub topic and then this json payload is being received by the Cloud functions.用户通过 Pub/Sub 主题以 JSON 格式发送数据,然后云功能正在接收此 json 有效负载。 There is a bit of processing on some of the data and then data is being stored in Datastore.对某些数据进行一些处理,然后将数据存储在 Datastore 中。

Now problem is that sometimes some other characters also received in the JSON payload string by Cloud function eg现在的问题是,有时 Cloud function 在 JSON 有效负载字符串中还会收到一些其他字符,例如

{'Data': 'ßTest'} #Already converted into UTF-8 by the user

so,when i do..所以,当我这样做时..

data = pubsub_message['Data']
print(data) # OUTPUT :=> 'ßTest'
print(type(data)) # OUTPUT :=> #'str'
data.decode('utf-8')

decode gives an exception that str doesnt have decode, which makes sense because its type is 'str'. decode 给出了 str 没有解码的异常,这是有道理的,因为它的类型是“str”。

Now what i am doing is i am encoding it as utf-8.现在我正在做的是我将它编码为 utf-8。

d=data.encode('utf-8')

Which gives me d back as type 'BYTES'.这给了我 d 类型'BYTES'。 and then i store it in Datastore.然后我将它存储在 Datastore 中。 Now when i check in datastore it is a wiered string and of type Blob.现在,当我签入数据存储时,它是一个 wiered 字符串,类型为 Blob。

Now my question is.Can i store it as it is in the Datastore without encoding it in 'utf-8'?现在我的问题是。我可以将它按原样存储在数据存储中而不用“utf-8”编码吗? or with encoding 'utf-8' in BLOB format in DATASTORE is ok?或者在 DATASTORE 中以 BLOB 格式编码“utf-8”可以吗?

As Best practices says:正如最佳实践所说:

Always use UTF-8 characters for properties of type string.对于字符串类型的属性,始终使用 UTF-8 字符。 A non-UTF-8 character in a property of type string could interfere with queries.字符串类型的属性中的非 UTF-8 字符可能会干扰查询。 If you need to save data with non-UTF-8 characters, use a byte string.如果您需要使用非 UTF-8 字符保存数据,请使用字节字符串。

Means that you have to store your data in UTF-8 or as a byte string.意味着您必须将数据存储在 UTF-8 或作为字节字符串中。

For Blob you store data as bytes as well.对于Blob ,您也将数据存储为字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM