展开全部
>>>s='''218996%S15173928K38024Kfgapp_108com.tencent.qq
2189934%S14191436K50888Kfgapp_108com.tencent.qq
2189949%S14183928K41584Kfgapp_108com.tencent.qq
2189928%S15176984K40240Kfgapp_108com.tencent.qq
218996%S15177004K40448Kfgapp_108com.tencent.qq
218996%S14176048K40564Kfgapp_108com.tencent.qq
2189910%S14176196K40472Kfgapp_108com.tencent.qq
218999%S14176232K40712Kfgapp_108com.tencent.qq
2189912%S14176288K40820Kfgapp_108com.tencent.qq
2189910%S14176288K40820Kfgapp_108com.tencent.qq
2189912%S16179376K40904Kfgapp_108com.tencent.qq'''
>>>open('a.txt','w').write(s)
>>>f=open('a.txt')
>>>f.read()
'218996%S15173928K38024Kfgapp_108com.tencent.qq\n2189934%S14191436K50888Kfgapp_108com.tencent.qq\n2189949%S14183928K41584Kfgapp_108com.tencent.qq\n2189928%S15176984K40240Kfgapp_108com.tencent.qq\n218996%S15177004K40448Kfgapp_108com.tencent.qq\n218996%S14176048K40564Kfgapp_108com.tencent.qq\n2189910%S14176196K40472Kfgapp_108com.tencent.qq\n218999%S14176232K40712Kfgapp_108com.tencent.qq\n2189912%S14176288K40820Kfgapp_108com.tencent.qq\n2189910%S14176288K40820Kfgapp_108com.tencent.qq\n2189912%S16179376K40904Kfgapp_108com.tencent.qq'
>>>pprint.pprint(map(lambdax:re.findall('\d+?+?(\d+%)+?S+?\d+?+?(\d+K)+?(\d+K)',x),s.split('\n')))
[[('6%','173928K','38024K')],
[('34%','191436K','50888K')],
[('49%','183928K','41584K')],
[('28%','176984K','40240K')],
[('6%','177004K','40448K')],
[('6%','176048K','40564K')],
[('10%','176196K','40472K')],
[('9%','176232K','40712K')],
[('12%','176288K','40820K')],
[('10%','176288K','40820K')],
[('12%','179376K','40904K')]]
>>>pprint.pprint(map(lambdax:re.findall('\d+?+?(\d+%)+?S+?\d+?+?(\d+K)+?(\d+K)',x),open('a.txt').read().split('\n')))
[[('6%','173928K','38024K')],
[('34%','191436K','50888K')],
[('49%','183928K','41584K')],
[('28%','176984K','40240K')],
[('6%','177004K','40448K')],
[('6%','176048K','40564K')],
[('10%','176196K','40472K')],
[('9%','176232K','40712K')],
[('12%','176288K','40820K')],
[('10%','176288K','40820K')],
[('12%','179376K','40904K')]]
>>>
如果觉得《python程序中想使用正则表达式_如何在python中使用正则表达式提取每行中需要的信息...》对你有帮助,请点赞、收藏,并留下你的观点哦!