2013-08-28 31 views
6

Tôi có một chuỗi được định dạng bson trong tệpParse bson string in python?

Tôi muốn đọc tệp đó và nhận được mã được mã hóa.

Tôi đã nhìn vào ví dụ ở đây:

>>> from bson import BSON 
>>> bson_string = BSON.encode({"hello": "world"}) 
>>> bson_string 
'\x16\x00\x00\x00\x02hello\x00\x06\x00\x00\x00world\x00\x00' 
>>> bson_string.decode() 
{u'hello': u'world'} 

từ http://docs.mongodb.org/meta-driver/latest/legacy/bson/

Nhưng những gì tôi có là nói:

string = '\x16\x00\x00\x00\x02hello\x00\x06\x00\x00\x00world\x00\x00' 

Và bây giờ tôi muốn phân tích json này? Tôi làm như thế nào? Cảm ơn


Bạn có thể thử để phân tích bson này được định dạng chuỗi:

s = """'\x93\x01\x00\x00\x02_id\x00\x1a\x00\x00\x00auromotiveengineering.com\x00\x04name_servers\x00_\x00\x00\x00\x020\x00\x17\x00\x00\x00ns-2.activatedhost.com\x00\x021\x00\x17\x00\x00\x00ns-1.activatedhost.com\x00\x022\x00\x17\x00\x00\x00ns-3.activatedhost.com\x00\x00\nreputation\x00\x04categories\x00\x05\x00\x00\x00\x00\x03host_act\x00\xd7\x00\x00\x00\x03bnMtMi5hY3RpdmF0ZWRob3N0LmNvbQ==\x00$\x00\x00\x00\x10seen_first\x00\x00,\xe7F\x10seen_last\x00\x80 \xebF\x00\x03bnMtMy5hY3RpdmF0ZWRob3N0LmNvbQ==\x00$\x00\x00\x00\x10seen_first\x00\x00,\xe7F\x10seen_last\x00\x80 \xebF\x00\x03bnMtMS5hY3RpdmF0ZWRob3N0LmNvbQ==\x00$\x00\x00\x00\x10seen_first\x00\x00,\xe7F\x10seen_last\x00\x80 \xebF\x00\x00\x00'""" 

Vì vậy, đây là những gì tôi đã làm: cho một 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 }""" 

Bây giờ, nạp chuỗi này

jsn = json.loads(s) 

bson_string = BSON.encode(jsn) 

And then i copy paste bson_string 

so bson_string = """'\x93\x01\x00\x00\x02_id\x00\x1a\x00\x00\x00auromotiveengineering.com\x00\x04name_servers\x00_\x00\x00\x00\x020\x00\x17\x00\x00\x00ns-2.activatedhost.com\x00\x021\x00\x17\x00\x00\x00ns-1.activatedhost.com\x00\x022\x00\x17\x00\x00\x00ns-3.activatedhost.com\x00\x00\nreputation\x00\x04categories\x00\x05\x00\x00\x00\x00\x03host_act\x00\xd7\x00\x00\x00\x03bnMtMi5hY3RpdmF0ZWRob3N0LmNvbQ==\x00$\x00\x00\x00\x10seen_first\x00\x00,\xe7F\x10seen_last\x00\x80 \xebF\x00\x03bnMtMy5hY3RpdmF0ZWRob3N0LmNvbQ==\x00$\x00\x00\x00\x10seen_first\x00\x00,\xe7F\x10seen_last\x00\x80 \xebF\x00\x03bnMtMS5hY3RpdmF0ZWRob3N0LmNvbQ==\x00$\x00\x00\x00\x10seen_first\x00\x00,\xe7F\x10seen_last\x00\x80 \xebF\x00\x00\x00 
""" 

và cho điều này khi tôi cố gắng .. nó ném một lỗi :(

Một chuỗi nơi mà tôi có một lỗi:

._idbrusselscityreporter.comcategorieshost_act�bnMzMC5kb21haW5jb250cm9sLmNvbQ==$seen_first�hLseen_last��NbnMyOS5kb21haW5jb250cm9sLmNvbQ==$seen_first�hLseen_last��Nname_serversA0ns30.domaincontrol.com1ns29.domaincontrol.com 

Trả lời

6

Bạn có thể làm điều này để khởi tạo một trường hợp BSON với một chuỗi:

>>> s = '\x16\x00\x00\x00\x02hello\x00\x06\x00\x00\x00world\x00\x00' 
>>> bson_obj = BSON(s) 
>>> bson_obj.decode() 
{u'hello': u'world'} 
+0

Xin chào .. Bạn có thể thử phân tích cú pháp chuỗi trong câu hỏi đã chỉnh sửa không? – Fraz

+1

Nó cho tôi một lỗi, 'objsize quá lớn'. Tôi không biết đó có phải là trường hợp hay không đúng. –

+0

Xin chào ... bạn có thể xem phần cập nhật của truy vấn của tôi không ??? – Fraz