{
"date": "18-03-29 06:04:47",
"data": {
"deviceType": 1,
"result": {
"resultType": 1
},
"capture": {
"gender": 0,
"smallImageUrl": "http://images-test.xlsdn.com/aW90X2Mzc19xZnQ5d2U1NVRyM3NTbEhLV0JVVBPT0=?auth_key=1522577086-0-0-27c9a7f9d9bccecbb18e3d6b58044fed",
"bigImageUrl": "http://images-test.xlsdn.com/aW90X2Mzc190ZncvdjVMVWJHNzdzcFdrUDZJMWV3PT0=?auth_key=1522577086-0-0-cd3e6b2a109bbb6f71727d4dc3674c92",
"faceId": "b9daccef-5c5b-4f8a-9f01-1875314fb7ec",
"age": 20,
"timestamp": 1522317682
},
"shopId": "23",
"deviceId": "ID0000801941060862520718"
}
}
#练习:用正则表达式方式判断deviceType对应的value值是否是整数,如果是整数把faceid截取出来
追加到文件内
import re
import json
def json_check(json_result,filename):
str=""
with open(json_result,"r") as f:
str=f.read()
try:
device=int(re.search(r'"deviceType":(.*?),',str).group(1).strip())
except TypeError,e:
return str(e)
except AttributeError,e:
return str(e)
except Exception,e:
return str(e)
faceId=re.search(r'"faceId":(.*?),',str).group(1).strip()
with open(filename,"a+") as f:
f.write(faceId)
json_check("e:\json_result.xml","e:\result.txt")