简体   繁体   English

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

[英]Fire store in python filling fields with random data


I have a question about python and firestore, I have conected python to fire store and I can add data however when I try to fill in a field in my database using a Varible all I get in the firebase console is a random string.我有一个关于 python 和 firestore 的问题,我已将 python 连接到 firestore,我可以添加数据但是当我尝试使用变量填充数据库中的字段时,我在 firebase 控制台中得到的只是一个随机字符串。


My code 我的代码

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)


Here is my firebase console这是我的 Firebase 控制台
Sorry, I don't have the needed reputation to embed an image but here is the link抱歉,我没有嵌入图像所需的声誉,但这里是链接


note 笔记
I am trying to load the data to put into the database from a server however I have verified this is not the issue. 我正在尝试从服务器加载数据以放入数据库,但是我已经验证这不是问题。 the first one of these url ib request is getting a name of the document however this works well but the second one is where I get the field data but the problem is not loading it off a server 这些 url ib 请求中的第一个是获取文档的名称,但是这很好用,但第二个是我获取字段数据的地方,问题不是从服务器加载它
Thanks! 谢谢!

The data is being base64-encoded .数据是base64 编码的

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

I don't use Firebase/Firestore but if the data is being automatically base64-encoded on write then most likely it will be automatically decoded when read.我不使用 Firebase/Firestore,但如果数据在写入时自动进行 base64 编码,那么很可能会在读取时自动解码。

If you need to manually decode it, note that base64.b64decode returns bytes , so you need to call .decode() on the bytes to get a str .如果您需要手动解码它,请注意base64.b64decode返回bytes ,因此您需要对bytes调用 .decode() 以获取str

This comment on github suggests that adding the u prefix to string literals will make Firestore encode as UTF-8 instead of base64. github上的这条评论表明,将u前缀添加到字符串文字将使 Firestore 编码为 UTF-8 而不是 base64。

So in your example:所以在你的例子中:

uname = u'Hello'

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

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