从resum中提取过去的年份

2024-04-26 05:47:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经写好了从简历中摘录过去一年的逻辑。我使用学位列表的查找来提取学位。你知道吗

下面是文本和代码的链接-https://github.com/karimkhanp/resumeparser

在CV中有了所有的学位之后,我进行了行分割并检查每一行。如果任何一行中存在任何程度,则检查同一行中是否有以19或20开头的4位数字。考虑一年吧。你知道吗

逻辑代码:

def get_passingyear(self, text, education):
    text_lines = text.splitlines()
    passing_year = []
    for line in text_lines:
        for degree in education:
            if degree in line:
                year = re.findall('\b(19|20)\d{2}\b', text)
                p_year = {}
                if len(year) > 1:
                    year = '-'.join(year)
                    p_year[degree]= year
                    break
                else:
                    p_year[degree]= year
                    break

有没有更好的方法来编写这个代码?我添加了break以在获取学位年份时退出循环。你知道吗

我很感激有没有更好的逻辑


Tags: 代码textin文本列表forifline
1条回答
网友
1楼 · 发布于 2024-04-26 05:47:31

您可以使用EAFP原理并尝试使用datetime模块:

import datetime

....

        if degree in line:
            try:
                year = re.findall('\b(19|20)\d{2}\b', text)
                # Try to make a date out of it
                datetime.date(year=int(year))
            except TypeError:
                # if it is not a date, you can treat it here
                pass

            ....

这样你就不会冒险得到一个不到一年的东西 如果所有的日期在这些文件中都有一个模式,那么可以使用strtime fromdatetime module从这个模式中获取日期

相关问题 更多 >