python 字符串查找并查找字符串,直到从 json 中找到 space
python string find and look up on string until found space from json
下面是我的数据。
'{"Data": {"a":5647953897,"b":"323299901059958183671030","c":1605858513465}}{"Data": {"a":5647953897,"b":"323299901059958183671030","c":1605858513465}}'
我的输出应该如下所示
[{"a":5647953897,"b":"323299901059958183671030","c":1605858513465},{"a":5647953897,"b":"323299901059958183671030","c":1605858513465}]
这应该可以解决您的问题。
from json import JSONDecoder, JSONDecodeError
import re
NOT_WHITESPACE = re.compile(r'[^\s]')
data = '''{"Data": {"a":5647953897,"b":"323299901059958183671030","c":1605858513465}}{"Data": {"a":5647953897,"b":"323299901059958183671030","c":1605858513465}}'''
def decode_stacked(document, pos=0, decoder=JSONDecoder()):
while True:
match = NOT_WHITESPACE.search(document, pos)
if not match:
return
pos = match.start()
try:
obj, pos = decoder.raw_decode(document, pos)
except JSONDecodeError:
raise
yield obj
for obj in decode_stacked(data):
print(obj)
下面是我的数据。
'{"Data": {"a":5647953897,"b":"323299901059958183671030","c":1605858513465}}{"Data": {"a":5647953897,"b":"323299901059958183671030","c":1605858513465}}'
我的输出应该如下所示
[{"a":5647953897,"b":"323299901059958183671030","c":1605858513465},{"a":5647953897,"b":"323299901059958183671030","c":1605858513465}]
这应该可以解决您的问题。
from json import JSONDecoder, JSONDecodeError
import re
NOT_WHITESPACE = re.compile(r'[^\s]')
data = '''{"Data": {"a":5647953897,"b":"323299901059958183671030","c":1605858513465}}{"Data": {"a":5647953897,"b":"323299901059958183671030","c":1605858513465}}'''
def decode_stacked(document, pos=0, decoder=JSONDecoder()):
while True:
match = NOT_WHITESPACE.search(document, pos)
if not match:
return
pos = match.start()
try:
obj, pos = decoder.raw_decode(document, pos)
except JSONDecodeError:
raise
yield obj
for obj in decode_stacked(data):
print(obj)