新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python3如何用re導(dǎo)出文本數(shù)據(jù)?
在小學(xué)做題的時(shí)候,如果題目中有相同的數(shù)值,我們還可以通過人工比對(duì)查找出來。不過現(xiàn)在要用電腦處理的數(shù)據(jù)很多,我們需要借助某種工具,來幫我們篩選出重復(fù)的數(shù)據(jù)或是文件名。那么學(xué)了python的我們有沒有什么解決辦法呢?今天小編就教大家用re導(dǎo)出文本數(shù)據(jù),具體舉例如下:

文本內(nèi)容如下,就是一個(gè)編譯后的map,我想提取里面的symbol信息,地址,以及在哪些模塊里面使用了
當(dāng)下面有多行時(shí),也就是在多個(gè).o文件中使用時(shí),怎么提取出每一個(gè).o
表達(dá)式是:
_([a-zA-Z0-9_]+)\s+([a-z0-9A-Z]{8})\s+defined\s+in\s+[a-zA-Z0-9_]+.o\s+section\s+.+\n\s+used in\s+([a-zA-Z0-9_]+.o)\s*\n\s*(\w+.o)\n\s*(\w+.o)
問題1:
當(dāng)需要匹配多個(gè)“ ******.o”時(shí)如何匹配
問題2:
如何把所有滿足條件的都匹配出來,
_PfTORQ_r_ThermEffCorrMult 000fe417 defined in torqmall.o section .bss used in torqmctl.o torqmrat.o _PeTORQ_GearState 000fe419 defined in torqmall.o section .bss used in torq_meth_jac.o torq_mulf_jac.o torqmgve.o torqmgvv.o etcdmtps.o _PeTORQ_GearStatePrev 000fe41a defined in torqmall.o section .bss _PeTORQ_GearStateDsrd 000fe41b defined in torqmall.o section .bss _VfTORQ_AXIS_RPM_W_11Brk 000fe41c defined in torqmall.o section .bss used in torqmdes.o tqdrmall.o
解決方法:
re.findall(pattern, string, flags=0)
范例:
>>> text = "He was carefully disguised but captured quickly by police." >>> re.findall(r"\w+ly", text) ['carefully', 'quickly']
測試:
In [1]: yourstr="""_PfTORQ_r_ThermEffCorrMult 000fe417 defined in torqmall.o section .bss
used in torqmctl.o
torqmrat.o
_PeTORQ_GearState 000fe419 defined in torqmall.o section .bss
used in torq_meth_jac.o
torq_mulf_jac.o
torqmgve.o
torqmgvv.o
etcdmtps.o
_PeTORQ_GearStatePrev 000fe41a defined in torqmall.o section .bss
_PeTORQ_GearStateDsrd 000fe41b defined in torqmall.o section .bss
_VfTORQ_AXIS_RPM_W_11Brk 000fe41c defined in torqmall.o section .bss
used in torqmdes.o
tqdrmall.o"""
In [2]: re.findall('\w+\.o',yourstr)
Out[2]:
['torqmall.o',
'torqmctl.o',
'torqmrat.o',
'torqmall.o',
'torq_meth_jac.o',
'torq_mulf_jac.o',
'torqmgve.o',
'torqmgvv.o',
'etcdmtps.o',
'torqmall.o',
'torqmall.o',
'torqmall.o',
'torqmdes.o',
'tqdrmall.o']
看完后小伙伴們會(huì)發(fā)現(xiàn),我們想要的.o的結(jié)果已經(jīng)出來啦,說明re.findall針對(duì)此類問題的解決有效果哦~更多Python學(xué)習(xí)推薦:PyThon學(xué)習(xí)網(wǎng)教學(xué)中心。
文章題目:創(chuàng)新互聯(lián)Python教程:python3如何用re導(dǎo)出文本數(shù)據(jù)?
瀏覽路徑:http://fisionsoft.com.cn/article/coccjdj.html


咨詢
建站咨詢
