概述
我在文件中有一個bson格式的字符串在python中解析bson字符串?
我想讀取該文件並獲取編碼的json。
我一直在尋找到這裏的例子:
>>> from bson import BSON
>>> bson_string = BSON.encode({"hello": "world"})
>>> bson_string
'x16x00x00x00x02hellox00x06x00x00x00worldx00x00'
>>> bson_string.decode()
{u'hello': u'world'}
但我有是說:
string = 'x16x00x00x00x02hellox00x06x00x00x00worldx00x00'
,現在我要分析此JSON? 我該怎麼做? 感謝
你可以嘗試解析這個BSON格式的字符串:
s = """'x93x01x00x00x02_idx00x1ax00x00x00auromotiveengineering.comx00x04name_serversx00_x00x00x00x020x00x17x00x00x00ns-2.activatedhost.comx00x021x00x17x00x00x00ns-1.activatedhost.comx00x022x00x17x00x00x00ns-3.activatedhost.comx00x00nreputationx00x04categoriesx00x05x00x00x00x00x03host_actx00xd7x00x00x00x03bnMtMi5hY3RpdmF0ZWRob3N0LmNvbQ==x00$x00x00x00x10seen_firstx00x00,xe7Fx10seen_lastx00x80 xebFx00x03bnMtMy5hY3RpdmF0ZWRob3N0LmNvbQ==x00$x00x00x00x10seen_firstx00x00,xe7Fx10seen_lastx00x80 xebFx00x03bnMtMS5hY3RpdmF0ZWRob3N0LmNvbQ==x00$x00x00x00x10seen_firstx00x00,xe7Fx10seen_lastx00x80 xebFx00x00x00'"""
所以這是我做過什麼: 給 一個jsonstring
s = """'{ "_id" : "auromotiveengineering.com", "categories" : [ ], "host_act" : { "bnMtMi5hY3RpdmF0ZWRob3N0LmNvbQ==" : { "seen_first" : 1189555200, "seen_last" : 1189814400 }, "bnMtMS5hY3RpdmF0ZWRob3N0LmNvbQ==" : { "seen_first" : 1189555200, "seen_last" : 1189814400 }, "bnMtMy5hY3RpdmF0ZWRob3N0LmNvbQ==" : { "seen_first" : 1189555200, "seen_last" : 1189814400 } }, "name_servers" : [ t"ns-2.activatedhost.com", t"ns-1.activatedhost.com", t"ns-3.activatedhost.com" ], "reputation" : null }"""
現在,加載這個字符串
jsn = json.loads(s)
bson_string = BSON.encode(jsn)
And then i copy paste bson_string
so bson_string = """'x93x01x00x00x02_idx00x1ax00x00x00auromotiveengineering.comx00x04name_serversx00_x00x00x00x020x00x17x00x00x00ns-2.activatedhost.comx00x021x00x17x00x00x00ns-1.activatedhost.comx00x022x00x17x00x00x00ns-3.activatedhost.comx00x00nreputationx00x04categoriesx00x05x00x00x00x00x03host_actx00xd7x00x00x00x03bnMtMi5hY3RpdmF0ZWRob3N0LmNvbQ==x00$x00x00x00x10seen_firstx00x00,xe7Fx10seen_lastx00x80 xebFx00x03bnMtMy5hY3RpdmF0ZWRob3N0LmNvbQ==x00$x00x00x00x10seen_firstx00x00,xe7Fx10seen_lastx00x80 xebFx00x03bnMtMS5hY3RpdmF0ZWRob3N0LmNvbQ==x00$x00x00x00x10seen_firstx00x00,xe7Fx10seen_lastx00x80 xebFx00x00x00
"""
,爲此當我嘗試..它拋出一個錯誤:(
另一個字符串,其中我有一個錯誤:
._idbrusselscityreporter.comcategorieshost_act�bnMzMC5kb21haW5jb250cm9sLmNvbQ==$seen_first�hLseen_last��NbnMyOS5kb21haW5jb250cm9sLmNvbQ==$seen_first�hLseen_last��Nname_serversA0ns30.domaincontrol.com1ns29.domaincontrol.com
2013-08-28
Fraz
最后
以上就是精明含羞草为你收集整理的python如何解析字符串内容_在python中解析bson字符串?的全部内容,希望文章能够帮你解决python如何解析字符串内容_在python中解析bson字符串?所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复