[英]how to add fields in google cloud fire store using python
[英]Fire store in python filling fields with random data
我有一个关于 python 和 firestore 的问题,我已将 python 连接到 firestore,我可以添加数据但是当我尝试使用变量填充数据库中的字段时,我在 firebase 控制台中得到的只是一个随机字符串。
import urllib import firebase_admin from firebase_admin import credentials from firebase_admin import firestore link = "example.com/test1" f = urllib.urlopen(link) uw = f.read() linky = "Example.com/test.txt" fa = urllib.urlopen(linky) uname = fa.read() print(uname) unname=uname # Use a service account uname="hello" cred = credentials.Certificate('key.json') firebase_admin.initialize_app(cred) #uw="hello" db = firestore.client() doc_ref = db.collection(u'treasure').document(uw) doc_ref.set({ u'found_by': uname #u'last': u'Lovelace', #u'born': 1815 }) #print(uuname) print(uname)
这是我的 Firebase 控制台
抱歉,我没有嵌入图像所需的声誉,但这里是链接
数据是base64 编码的。
>>> s = 'aGVsbG8='
>>> base64.b64decode(s)
b'hello'
我不使用 Firebase/Firestore,但如果数据在写入时自动进行 base64 编码,那么很可能会在读取时自动解码。
如果您需要手动解码它,请注意base64.b64decode
返回bytes
,因此您需要对bytes
调用 .decode() 以获取str
。
github上的这条评论表明,将u
前缀添加到字符串文字将使 Firestore 编码为 UTF-8 而不是 base64。
所以在你的例子中:
uname = u'Hello'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.