import re import json import string class IdCardStraight: """ """ def __init__(self, result): self.result = [ i.replace(" ", "").translate(str.maketrans("", "", string.punctuation)) for i in result ] self.out = {"Data": {"FrontResult": {}}} self.res = self.out["Data"]["FrontResult"] self.res["Name"] = "" self.res["number"] = "" self.res["type2"] = "" self.res["Gender"] = "" self.res["address"] = "" self.res["birthplace"]="" self.res["native"]="" self.res["blood"]="" self.res["religion"]="" def name(self): """ 姓名 """ # print(self) addString=[] for i in range(len(self.result)): txt = self.result[i] print(txt) if ("姓名" or "名" in txt) and len(txt) > 2: txt=txt.replace("姓名:","") addString.append(txt) else: txt=self.result[i+1].replace("名","").replace("常住人口登记卡","") addString.append(txt) self.res["Name"]="".join(addString) break def sex(self): """ 性别女 """ for i in range(len(self.result)): txt = self.result[i] if "男" in txt: self.res["Gender"] = "男" elif "女" in txt: self.res["Gender"] = "女" def type2(self): """ 户口类型 """ for i in range(len(self.result)): txt=self.result[i] if "非农" in txt: self.res["type2"]="非农业户口" elif "农" in txt: self.res["type2"]="农业户口" def address(self): """ 住址 """ addString = [] for i in range(len(self.result)): txt = self.result[i] txt = txt.replace("号码", "") if "派出所" in txt: txt = "temp" # 地址 if "何时" in txt: txt = "temp" if ( "住址" in txt or "址" in txt or "省" in txt or "市" in txt or "县" in txt or "街" in txt or "乡" in txt or "村" in txt or "镇" in txt or "区" in txt or "城" in txt or "组" in txt or "号" in txt ): if "住址" in txt or "省" in txt or "址" in txt: addString.insert(0, txt.split("址")[-1]) else: addString.append(txt) self.result[i] = "temp" #print(addString) if len(addString) > 0: self.res["address"] = "".join(addString) else: self.res["address"] = "" def blood(self): """ 血型 """ addString=[] for i in range(len(self.result)): txt=self.result[i] if "血型" in txt: txt=txt.replace("血型:","") if len(txt)==0: txt=self.result[i+1].replace("血型:","") print(txt) addString.append(txt) self.res["blood"]="".join(addString) def religion(self): """ 宗教 """ addString=[] for i in range(len(self.result)): txt=self.result[i] if "宗教信仰" in txt: txt=txt.replace("宗教信仰:","") if len(txt) == 0: txt=self.result[i+1] addString.append(txt) self.res["religion"]="".join(addString) def number(self): """ 号码 """ for i in range(len(self.result)): txt = self.result[i] # 号码 if "X" in txt or "x" in txt: res = re.findall("\d*[X|x]", txt) else: res = re.findall("\d{16,18}", txt) if len(res) > 0: if len(res[0]) == 18: self.res["number"] = res[0].replace("号码", "") self.res["Gender"] = "男" if int(res[0][16]) % 2 else "女" break def run(self): print(self) self.name() self.sex() self.type2() self.address() self.blood() self.religion() self.number() #print(self.out) return json.dumps(self.out)