python如何使用findall()提取字段?

时间:2017-10-20 07:08:50

标签: python

                            staffs:
                            50 - 100


                            year_turnover:
                            0

                            url:
                            http://www.chinaanigu.com

如何re.findall()year_turnover:after和staffs:after

2 个答案:

答案 0 :(得分:0)

剥离所有不需要的转义字符后,跟随正则表达式'可用于提取所需数据:

import re

sample_text = """
staffs:
50 - 100

year_turnover:
0

url:
http://www.chinaanigu.com
"""

print re.findall(r"staffs:\n(.*)", sample_text) # catches anything followed by staffs:
>>> ['50 - 100']
print re.findall(r"year_turnover:\n(.*)", sample_text)
>>> ['0']

答案 1 :(得分:0)

import re
a = """
                            staffs:
                            50 - 100


                            year_turnover:
                            0

                            url:
                            http://www.chinaanigu.com
"""
b = re.findall("staffs:\s+(\d.+\d)", a)
c = re.findall("year_turnover:\s+(\d)", a)
print(b)
print(c)