2013-08-28 4 views
6

파일에 bson 형식의 문자열이 있습니다.파이썬에서 bson 문자열을 파싱 하시겠습니까?

해당 파일을 읽고 인코딩 된 json을 가져오고 싶습니다.

여기의 예에보고했다 :

>>> 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'} 

http://docs.mongodb.org/meta-driver/latest/legacy/bson/에서하지만 내가 가지고있는 것은 말할 수 있습니다 :

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

을 지금은이 JSON을 구문 분석하려면? 어떻게해야합니까? 감사


당신이 bson 형식의 문자열을 구문 분석을 시도 할 수

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'""" 

을 그래서 내가 무슨 짓을 : 는 이제 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 = """'\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 
""" 
내가하려고 할 때

이에 대한은 ... 그것은 오류가 :(발생

내가 오류가 또 다른 문자열 :

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

답변

6

당신은 문자열과 함께 BSON 인스턴스를 초기화하기 위해이 작업을 수행 할 수 있습니다

>>> 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

안녕하세요 .. 편집 한 질문에서 문자열을 구문 분석 할 수 있습니까? – Fraz

+1

그것은'objsize too large'라는 에러를 내게합니다. 그게 사실인지 아니면 단지 형식이 틀린 지 나는 모른다. –

+0

안녕하세요 ... 내 쿼리의 업데이트 된 부분을 살펴볼 수 있습니까 ??? – Fraz

관련 문제