common2.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import re
  2. import json
  3. import string
  4. class IdCardStraight:
  5. """
  6. """
  7. def __init__(self, result):
  8. self.result = [
  9. i.replace(" ", "").translate(str.maketrans("", "", string.punctuation))
  10. for i in result
  11. ]
  12. self.out = {"Data": {"FrontResult": {}}}
  13. self.res = self.out["Data"]["FrontResult"]
  14. self.res["Name"] = ""
  15. self.res["number"] = ""
  16. self.res["type2"] = ""
  17. self.res["Gender"] = ""
  18. self.res["address"] = ""
  19. self.res["birthplace"]=""
  20. self.res["native"]=""
  21. self.res["blood"]=""
  22. self.res["religion"]=""
  23. def name(self):
  24. """
  25. 姓名
  26. """
  27. # print(self)
  28. addString=[]
  29. for i in range(len(self.result)):
  30. txt = self.result[i]
  31. print(txt)
  32. if ("姓名" or "名" in txt) and len(txt) > 2:
  33. txt=txt.replace("姓名:","")
  34. addString.append(txt)
  35. else:
  36. txt=self.result[i+1].replace("名","").replace("常住人口登记卡","")
  37. addString.append(txt)
  38. self.res["Name"]="".join(addString)
  39. break
  40. def sex(self):
  41. """
  42. 性别女
  43. """
  44. for i in range(len(self.result)):
  45. txt = self.result[i]
  46. if "男" in txt:
  47. self.res["Gender"] = "男"
  48. elif "女" in txt:
  49. self.res["Gender"] = "女"
  50. def type2(self):
  51. """
  52. 户口类型
  53. """
  54. for i in range(len(self.result)):
  55. txt=self.result[i]
  56. if "非农" in txt:
  57. self.res["type2"]="非农业户口"
  58. elif "农" in txt:
  59. self.res["type2"]="农业户口"
  60. def address(self):
  61. """
  62. 住址
  63. """
  64. addString = []
  65. for i in range(len(self.result)):
  66. txt = self.result[i]
  67. txt = txt.replace("号码", "")
  68. if "派出所" in txt:
  69. txt = "temp"
  70. # 地址
  71. if "何时" in txt:
  72. txt = "temp"
  73. if (
  74. "住址" in txt
  75. or "址" in txt
  76. or "省" in txt
  77. or "市" in txt
  78. or "县" in txt
  79. or "街" in txt
  80. or "乡" in txt
  81. or "村" in txt
  82. or "镇" in txt
  83. or "区" in txt
  84. or "城" in txt
  85. or "组" in txt
  86. or "号" in txt
  87. ):
  88. if "住址" in txt or "省" in txt or "址" in txt:
  89. addString.insert(0, txt.split("址")[-1])
  90. else:
  91. addString.append(txt)
  92. self.result[i] = "temp"
  93. #print(addString)
  94. if len(addString) > 0:
  95. self.res["address"] = "".join(addString)
  96. else:
  97. self.res["address"] = ""
  98. def blood(self):
  99. """
  100. 血型
  101. """
  102. addString=[]
  103. for i in range(len(self.result)):
  104. txt=self.result[i]
  105. if "血型" in txt:
  106. txt=txt.replace("血型:","")
  107. if len(txt)==0:
  108. txt=self.result[i+1].replace("血型:","")
  109. print(txt)
  110. addString.append(txt)
  111. self.res["blood"]="".join(addString)
  112. def religion(self):
  113. """
  114. 宗教
  115. """
  116. addString=[]
  117. for i in range(len(self.result)):
  118. txt=self.result[i]
  119. if "宗教信仰" in txt:
  120. txt=txt.replace("宗教信仰:","")
  121. if len(txt) == 0:
  122. txt=self.result[i+1]
  123. addString.append(txt)
  124. self.res["religion"]="".join(addString)
  125. def number(self):
  126. """
  127. 号码
  128. """
  129. for i in range(len(self.result)):
  130. txt = self.result[i]
  131. # 号码
  132. if "X" in txt or "x" in txt:
  133. res = re.findall("\d*[X|x]", txt)
  134. else:
  135. res = re.findall("\d{16,18}", txt)
  136. if len(res) > 0:
  137. if len(res[0]) == 18:
  138. self.res["number"] = res[0].replace("号码", "")
  139. self.res["Gender"] = "男" if int(res[0][16]) % 2 else "女"
  140. break
  141. def run(self):
  142. print(self)
  143. self.name()
  144. self.sex()
  145. self.type2()
  146. self.address()
  147. self.blood()
  148. self.religion()
  149. self.number()
  150. #print(self.out)
  151. return json.dumps(self.out)