繁体   English   中英

用随机数据填充字段的python中的Fire存储

[英]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 控制台
抱歉,我没有嵌入图像所需的声誉,但这里是链接


笔记
我正在尝试从服务器加载数据以放入数据库,但是我已经验证这不是问题。 这些 url ib 请求中的第一个是获取文档的名称,但是这很好用,但第二个是我获取字段数据的地方,问题不是从服务器加载它
谢谢!

数据是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.

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