现有字符串
one_str = "19:02:00:252 [N]-[15505-789]-[27_android_6.2.10]-[v]-[releaseEdition]-[perf]startType=0,event=start_time_monitor,params={mStartAd=819, mIsFromLauncher=1, baseLibraryFinish=286, baseComponentFinish=318, mAppStart=1596970904094, mEndAd=6063, businessComponentFinish=318, mFirstScreenFinish=6154, frameworkInitFinish=216, mFirstActivityCreate=342, readyToStartFinish=318, mHasAd=1, frameworkInitStart=47}"
使用python的正则如何从上面的字符串中提取所需的key和value?
或者如何快速的将字符串中的等号两边变成字典?
有待学习和研究
参考文章:Python 正则表达式匹配两个指定字符串中间的内容
import re
txt='@font-face{font-family:"customfont"; src:url(data:application/font-woff;charset=utf-8;base64,d09GRgABAAAAACHMAHUrlQa1/U/GjaKLvBmPZEW9aysrJJd8fcdtKlebEw+i+9+ss+zY9V28te/XU+6HyW/QK0Vmv==)'
bs64_str = re.findall("charset=utf-8;base64,(.*?)\)", txt)[0]
print(bs64_str)
执行结果:
d09GRgABAAAAACHMAHUrlQa1/U/GjaKLvBmPZEW9aysrJJd8fcdtKlebEw+i+9+ss+zY9V28te/XU+6HyW/QK0Vmv==
参考上面文章后自己的代码是:
import sys
import os
import re
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
def findValue(key, string):
value = re.findall(key+"=(.*?)[}|,]",string)[0]
return value
if __name__ == '__main__':
one_str = "19:02:00:252 [N]-[15505-789]-[27_android_6.2.10]-[v]-[releaseEdition]-[perf]startType=0,event=start_time_monitor,params={mStartAd=819, mIsFromLauncher=1, baseLibraryFinish=286, baseComponentFinish=318, mAppStart=1596970904094, mEndAd=6063, businessComponentFinish=318, mFirstScreenFinish=6154, frameworkInitFinish=216, mFirstActivityCreate=342, readyToStartFinish=318, mHasAd=1, frameworkInitStart=47}"
print(findValue("startType",one_str))
print(findValue("event", one_str))
print(findValue("mStartAd", one_str))
print(findValue("baseLibraryFinish", one_str))
print(findValue("frameworkInitStart", one_str))
运行结果是:
/usr/local/bin/python3.7 /Users/baosenzhang/PycharmProjects/HelloPython/src/HelloPython.py
start_time_monitor
现有字符串one_str = "{'buildType': u'releaseEdition', 'params': {u'network_process_cost': 441, u'network_process_end': 351558853, u'jump_activity_start': 351558329, u'pre_process_cost': 9, u'pre_process_end': 351558412, u'cold_start_cost': 0, u'cold_start_end'
【Android环境】No toolchains found in the NDK toolchains folder for ABI with prefix: arm-linux-androidea