我有一个关于python和firestore的问题,我已经将python设置为fire store,但是我可以添加数据,但是当我尝试使用Varible填充数据库中的字段时,我在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控制台Sorry, I don't have the needed reputation to embed an image but here is the link
注意我正在尝试从服务器加载要放入数据库的数据,但是我已经证实这不是问题。这些url ib请求中的第一个是获取文档的名称,但是这很好用,但是第二个是我获取字段数据的地方,但是问题是没有从服务器上加载它。谢谢!

最佳答案

数据为base64-encoded

>>> s = 'aGVsbG8='
>>> base64.b64decode(s)
b'hello'


我不使用Firebase / Firestore,但是如果数据在写入时被自动进行base64编码,则很可能在读取时会被自动解码。

如果需要手动解码,请注意base64.b64decode返回bytes,因此您需要在字节上调用.decode()以获得str

github的评论表明,在字符串文字中添加u前缀将使Firestore编码为UTF-8而不是base64。

因此,在您的示例中:

uname = u'Hello'

关于python - 火在python中用随机数据填充字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55786051/

10-12 16:50
查看更多