12345678910111213141516171819202122232425262728293031323334353637 |
- import re
- class parser:
- def __init__(self):
- print("Address of self = ",id(self))
- def check_image_count(self,image_list, text):
- pair_obj = findTag(text)
- if pair_obj['code'] == 1:
- if len(image_list)!=len(pair_obj['pair']):
- return {'msg':'圖片與標籤數量不一致!','code':-1}
- else:
- return {code:1}
- else:
- return pair_obj
-
- def findTag(self,text):
- left_tag = [m.start() for m in re.finditer('{', text)]
- right_tag = [m.start() for m in re.finditer('}', text)]
- if len(left_tag)!=len(right_tag):
- return {'msg':'圖片標籤錯誤,左右數量不符','code':-1}
- pair = []
- for idx in range(len(left_tag)):
- pair.append({'left':left_tag[idx],'right':right_tag[idx]})
- obj = {'code':1,'pair':pair}
- return obj
- def replace_list(self,text):
- rep_ls = []
- pair_obj = self.findTag(text)
- for p in pair_obj['pair']:
- rep_ls.append(text[p['left']:p['right']+1])
- return rep_ls
-
-
|