从字符串(python)

时间:2018-05-20 13:59:59

标签: python

我有一个示例字符串: 'Last year's Fortune rank: No.3 2016 revenue $215.6 billion One-year Revenue Change: -7.7%'

我想从这些字符串中提取特定信息,以便将它们放在某些cateories下的python DataFrame中,例如:

Last year's Fortune rank: 3 2016 revenue ($B): 215.6 One-year revenue change: -7.7%

有没有办法在字符串中搜索某些文字并在找到它之后返回下一个字?

2 个答案:

答案 0 :(得分:1)

这样的事情怎么样?

s = "Last year's Fortune rank: No.3 2016 revenue $215.6 billion One-year Revenue Change: -7.7%"

import re
expression = re.compile(r"Last year's Fortune rank: No.(?P<rank>\d+) +2016 revenue \$(?P<revenue>[.0-9]+) billion One-year Revenue Change: (?P<revchange>[-.0-9]+)%")

m = expression.match(s)
print(m.groupdict())

哪个输出:

{'rank': '3', 'revenue': '215.6', 'revchange': '-7.7'}

当然,你可以用字典做任何你喜欢的事情

答案 1 :(得分:0)

这不是很干净,但它完成了工作:

s = 'Last year''s Fortune rank: No.3 2016 revenue $215.6 billion One-year Revenue Change: -7.7%'
print('Last year''s Fortune rank:', s.split('No.')[1].split()[0])
print('2016 revenue ($B):', s.split('$')[1].split()[0])
print('One-year revenue change:', s.split(':')[-1])

输出:

Last years Fortune rank: 3
2016 revenue ($B): 215.6
One-year revenue change:  -7.7%