日志样板(test.log):
[2019-07-14 00:04:31][/data/www/php/test.php][452][dalu]{"appid":421,"shop_id":"1060343","page":"index"}
[2019-07-14 00:24:02][/data/www/php/test.php][452][dalu]{"appid":420,"shop_id":"1029559","page":"index"}
[2019-07-14 00:24:04][/data/www/php/test.php][452][dalu]{"appid":420,"shop_id":"1029559","page":"index"}
[2019-07-14 00:37:06][/data/www/php/test.php][452][dalu]{"appid":420,"shop_id":"1048658","page":"index"}
[2019-07-14 00:37:08][/data/www/php/test.php][452][dalu]{"appid":420,"shop_id":"1048658","page":"index"}
需求:
提取appid,shop_id,page的值;
import re
with open("test.log","r",encoding="utf-8")as f:
for line in f:
V = re.match(r'.*\"appid\":(\d+),\"shop_id\":\"(\d+)\",\"page\":\"(\w+)\"*', line)
appid = V.group(1)
shop_id = V.group(2)
page = V.group(3)
print("appid :",appid, "shop_id :", shop_id, "page :",page)
运行结果: