仅基于外部括号的拆分字符串?

时间:2017-12-15 00:21:57

标签: python

我有这个字符串:

{"type":"summary","symbol":"SPY","open":"267.09","high":"267.22",
"low":"265.6", "prevClose":"266.75","close":"265.66"}
{"type":"quote","symbol":"SPY","bid":265.38,"bidsz":3,
"bidexch":"Q","biddate":"1513293904000","ask":265.42,
"asksz":45,"askexch":"P","askdate":"1513294015000"}
{"type":"summary","symbol":"SPY","open":"267.09",
"high":"267.22","low":"265.6","prevClose":"266.75","close":"265.66"}

如果我这样做:     类型(字符串)

我明白了:

<type 'unicode'> 

如果我这样做:

type(firstString)

其中firstString只是字符串三个部分中的第一部分,我得到:

<type 'unicode'>

使用Python,我如何根据外部括号分割它,这样我们从这一个字符串中获得三个字符串,每个字符串的形式为“{...}”?

2 个答案:

答案 0 :(得分:0)

您可以在换行符上拆分字符串并使用json模块解析它。

import json

s = '''{"type":"summary","symbol":"SPY","open":"267.09","high":"267.22","low":"265.6","prevClose":"266.75","close":"265.66"}
{"type":"quote","symbol":"SPY","bid":265.38,"bidsz":3,"bidexch":"Q","biddate":"1513293904000","ask":265.42,"asksz":45,"askexch":"P","askdate":"1513294015000"}
{"type":"summary","symbol":"SPY","open":"267.09","high":"267.22","low":"265.6","prevClose":"266.75","close":"265.66"}'''

[json.loads(line) for line in s.split('\n')]

# returns:
[{'close': '265.66',
  'high': '267.22',
  'low': '265.6',
  'open': '267.09',
  'prevClose': '266.75',
  'symbol': 'SPY',
  'type': 'summary'},
 {'ask': 265.42,
  'askdate': '1513294015000',
  'askexch': 'P',
  'asksz': 45,
  'bid': 265.38,
  'biddate': '1513293904000',
  'bidexch': 'Q',
  'bidsz': 3,
  'symbol': 'SPY',
  'type': 'quote'},
 {'close': '265.66',
  'high': '267.22',
  'low': '265.6',
  'open': '267.09',
  'prevClose': '266.75',
  'symbol': 'SPY',
  'type': 'summary'}]

答案 1 :(得分:0)

如果没有嵌套对象,并且部件之间无法保证换行,正如您所说,它就像拆分}一样简单:

your_string = '''{"type":"summary","symbol":"SPY","open":"267.09","high":"267.22",
"low":"265.6", "prevClose":"266.75","close":"265.66"}
{"type":"quote","symbol":"SPY","bid":265.38,"bidsz":3,
"bidexch":"Q","biddate":"1513293904000","ask":265.42,
"asksz":45,"askexch":"P","askdate":"1513294015000"}
{"type":"summary","symbol":"SPY","open":"267.09",
"high":"267.22","low":"265.6","prevClose":"266.75","close":"265.66"}'''

substrings = [part.strip() + "}" for part in your_string.split("}") if part.strip()]
# ['{"type":"summary","symbol":"SPY","open":"267.09","high":"267.22",
#  "low":"265.6", "prevClose":"266.75","close":"265.66"}',
#  '{"type":"quote","symbol":"SPY","bid":265.38,"bidsz":3,
#  "bidexch":"Q","biddate":"1513293904000","ask":265.42,
#  "asksz":45,"askexch":"P","askdate":"1513294015000"}',
#  '{"type":"summary","symbol":"SPY","open":"267.09",
#  "high":"267.22","low":"265.6","prevClose":"266.75","close":"265.66"}']

或者您可以立即将这些部分解析为单独的Python词典:

dicts = [json.loads(part + "}") for part in your_string.split("}") if part.strip()]
# [{'open': '267.09', 'high': '267.22', 'prevClose': '266.75', 'type': 'summary',
#   'close': '265.66', 'low': '265.6', 'symbol': 'SPY'},
#  {'askdate': '1513294015000', 'bid': 265.38, 'asksz': 45, 'type': 'quote',
#   'ask': 265.42, 'bidsz': 3, 'bidexch': 'Q', 'biddate': '1513293904000',
#   'askexch': 'P', 'symbol': 'SPY'},
#  {'open': '267.09', 'high': '267.22', 'prevClose': '266.75', 'type': 'summary',
#   'close': '265.66', 'low': '265.6', 'symbol': 'SPY'}]