#python解析字符串中的省市区字符串 #str4 = "XX省XX市辛桥乡赵庄村XX号" #str4 = "XX省XX市XX区八卦二路XX号XX栋XX楼" #str4 = "XX市XX区老山东里XX栋XX门XX号" #str4 = "XX省XX县XX镇张大楼行政村张大楼自然村XXX号" str4 = "XX市XX区侨雅新街XX号XXX房" # python怎么判断字符串是否为空 if len(str4) > 0: try: ##正常情况下 index1 = str4.index("省"); addr1 = str4[:str4.index("省")+1] print("main-addr1="+addr1) index2 = str4.index("市"); addr2 = str4[index1+1:str4.index("市")+1] print("main-addr2=" + addr2) index3 = str4.index("区"); addr3 = str4[index2+1:str4.index("区")+1] print("main-addr3=" + addr3) except ValueError as e: try: index2 = str4.index("市"); addr2 = str4[:str4.index("市")+1] print("err-addr2=" + addr2) index3 = str4.index("区"); addr3 = str4[index2+1:str4.index("区")+1] print("err-addr3=" + addr3) except ValueError as e: try: index3 = str4.index("区"); addr3 = str4[:str4.index("区")+1] print("err-addr3=" + addr3) except ValueError as e: try: index3 = str4.index("县"); addr3 = str4[:str4.index("县") + 1] print("err-addr3-2=" + addr3) except ValueError as e: print("not find")
标签:index,python,str4,print,XX,addr2,addr3,字符串,省市区 From: https://www.cnblogs.com/oktokeep/p/16588056.html