parser.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import re
  2. class parser:
  3. def __init__(self):
  4. print("Address of self = ",id(self))
  5. def check_image_count(self,image_list, text):
  6. pair_obj = findTag(text)
  7. if pair_obj['code'] == 1:
  8. if len(image_list)!=len(pair_obj['pair']):
  9. return {'msg':'圖片與標籤數量不一致!','code':-1}
  10. else:
  11. return {code:1}
  12. else:
  13. return pair_obj
  14. def findTag(self,text):
  15. left_tag = [m.start() for m in re.finditer('{', text)]
  16. right_tag = [m.start() for m in re.finditer('}', text)]
  17. if len(left_tag)!=len(right_tag):
  18. return {'msg':'圖片標籤錯誤,左右數量不符','code':-1}
  19. pair = []
  20. for idx in range(len(left_tag)):
  21. pair.append({'left':left_tag[idx],'right':right_tag[idx]})
  22. obj = {'code':1,'pair':pair}
  23. return obj
  24. def replace_list(self,text):
  25. rep_ls = []
  26. pair_obj = self.findTag(text)
  27. for p in pair_obj['pair']:
  28. rep_ls.append(text[p['left']:p['right']+1])
  29. return rep_ls