python如何获取非连续括号内的值

使用手写文字识别orc来获取题目的答案,有的识别结果括号的内容是连续的,就能够提取,如果非连续括号该如何提取

下面是我当前获取连续文本括号内的内容

[{'confidence': 1,
  'location': {'top_left': {'x': 0, 'y': 14},
   'right_bottom': {'x': 339, 'y': 33}},
  'word': [{'content': '(B)1.-What___is'}, {'content': 'it?'}]},
 {'confidence': 1,
  'location': {'top_left': {'x': 79, 'y': 58},
   'right_bottom': {'x': 258, 'y': 89}},
  'word': [{'content': "-It's"}, {'content': 'Tuesday.'}]},
 {'confidence': 1,
  'location': {'top_left': {'x': 78, 'y': 115},
   'right_bottom': {'x': 193, 'y': 147}},
  'word': [{'content': 'A.today'}]},
 {'confidence': 1,
  'location': {'top_left': {'x': 399, 'y': 115},
   'right_bottom': {'x': 481, 'y': 155}},
  'word': [{'content': 'B.day'}]},
 {'confidence': 1,
  'location': {'top_left': {'x': 703, 'y': 128},
   'right_bottom': {'x': 785, 'y': 160}},
  'word': [{'content': 'C.time'}]}]


for result_word in result_line['word']:
            findall_1 = re.findall(r'[(](.*?)[)]', result_word['content'])
            findall_2 = re.findall(r'[(](.*?)[)]', result_word['content'])
            findall_3 = re.findall(r'[(](.*?)[)]', result_word['content'])
            findall_4 = re.findall(r'[(](.*?)[)]', result_word['content'])
            if(len(findall_1)!=0):
                return findall_1[0]
            elif(len(findall_2)!=0):
                return findall_2[0]
            elif(len(findall_3)!=0):
                return findall_3[0]
            elif(len(findall_4)!=0):
                return findall_4[0]

当识别的结果出现以下情况,变会识别失败。

img


[{'confidence': 1,
  'location': {'top_left': {'x': 0, 'y': 0},
   'right_bottom': {'x': 417, 'y': 67}},
  'word': [{'content': '('},
   {'content': 'A'},
   {'content': ')'},
   {'content': '1'},
   {'content': '.'},
   {'content': '-'},
   {'content': 'What'},
   {'content': '_'},
   {'content': 'is'},
   {'content': 'it'},
   {'content': '?'}]},
 {'confidence': 1,
  'location': {'top_left': {'x': 127, 'y': 78},
   'right_bottom': {'x': 337, 'y': 130}},
  'word': [{'content': "-It's"}, {'content': 'Tuesday.'}]},
 {'confidence': 1,
  'location': {'top_left': {'x': 111, 'y': 146},
   'right_bottom': {'x': 241, 'y': 193}},
  'word': [{'content': 'A.today'}]},
 {'confidence': 1,
  'location': {'top_left': {'x': 479, 'y': 162},
   'right_bottom': {'x': 593, 'y': 208}},
  'word': [{'content': 'B.day'}]},
 {'confidence': 1,
  'location': {'top_left': {'x': 847, 'y': 179},
   'right_bottom': {'x': 945, 'y': 212}},
  'word': [{'content': 'C.time'}]}]

这种情况下该如何获取到 括号里面的内容呢

像刚才讲的,获取content然后遍历结果,获取嵌套content结果的值

直接拼接一下,然后正则匹配,是最简单的