知行编程网知行编程网  2023-01-12 18:30 知行编程网 隐藏边栏  1 
文章评分 0 次,平均分 0.0
导语: 本文主要介绍了关于python3如何用re导出文本数据?的相关知识,包括python文本解析,以及python写入文本这些编程知识,希望对大家有参考作用。


python3如何使用re导出文本数据?

小学做题的时候,如果题中有相同​​的值,我们也可以通过人工比对找出来。但是现在有大量的数据要被计算机处理,我们需要某种工具来帮助我们过滤掉重复的数据或者文件名。那么如果我们学过python,有什么解决办法吗?今天小编就教大家如何使用re导出文本数据。具体例子如下:

正文内容如下,为编图。我想提取符号信息,地址,以及其中使用了哪些模块。



当下面有多行时,也就是在多个

.o文件中使用时,怎么提取出每一个.o


表达式是:

_([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:



当需要匹配多个

“ ******.o”时如何匹配



问题

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']


看完后小伙伴们会发现,我们想要的.o的结果已经出来啦,说明re.

findall针对此类问题的解决有效果哦~


更多Python学习推荐:




本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

知行编程网
知行编程网 关注:1    粉丝:1
这个人很懒,什么都没写
扫一扫二维码分享