首页 > 解决方案 > 如何通过在python中调用文件中的函数名来打印文件名

问题描述

这是一个基于简历筛选的程序。在这个程序中,我为每个 CV 打分以对它们进行排名。现在输出只是给了我我想要的分数,它给了我文件名。该程序是:

import re
import fitz
import os

# Create an array "zs[]" that store the score values
zs = []

# call the Resume files by calling the folder name
for filename in os.listdir('resume/'):

    # Select only PDF files
    if filename.endswith('.pdf'):
        print(filename)
        os.chdir('C:/Users/M. Abrar Hussain/Desktop/cv/resume')
        pdfFileObj = open(filename, 'rb')

        # Extract the text Data from resume files
        with fitz.open(pdfFileObj) as doc:
            text = ""
            for page in doc:
                text += page.getText()
            print(text)

            # Splitting the Resume Data into many indexes of Array
            p = doc.loadPage(0)
            p_text = p.getText()
            p_lines = p_text.splitlines()

            # Split the information and the data
            split_lst = [i.split(': ', 1) for i in p_lines]
            d = {a: b.rstrip() for a, b in split_lst}

            # Naming the information part
            f = d["Name"]
            g = d["GPA"]
            h = d["Skills"]
            i = d["Experience"]
            p = re.findall(r"[-+]?\d*\.\d+|\d+", i)

            # search the keywords with the data that extract from resume
            search_keywords = ['Laravel', 'Java', 'Python']
            search_exp = ['1', '1.5', '2', '2.5', '3']
            search_gpa = ['2.5', '2.6', '2.7', '2.8', '2.9', '3.0', '3.1', '3.2', '3.3', '3.4', '3.5', '3.6', '3.7',
                          '3.8', '3.9', '4.0']

            # Comparing GPA data with the keywords
            lst = []
            for gpa in search_gpa:
                if gpa in g:
                    lst.append(gpa)

            # Comparing Skills data with keywords
            lst1 = []
            for word in search_keywords:
                if word in h:
                    lst1.append(word)

            # Comparing Experience data with keywords
            lst2 = []
            for exp in search_exp:
                if exp in p:
                    lst2.append(exp)

            # Scoring the Extracted data to see the best resume
            score = 0
            w1 = []

            # Scoring the GPA
            for w1 in lst:
                if '3.0' <= w1 < '3.5':
                    score += 1
                if '3.5' <= w1 <= '4':
                    score += 2

            # Scoring the SKills
            for w1 in lst1:
                if w1 == 'Laravel':
                    score += 2
                if w1 == 'Python':
                    score += 2
                if w1 == 'Java':
                    score += 1

            # Scoring the Experience
            for w1 in lst2:
                if '2.5' <= w1 < '3':
                    score += 0.5
                if '3' <= w1 < '3.5':
                    score += 1
                if '3.5' <= w1:
                    score += 2

            # store score values in an array
            tt = zs.append(score)

            print("%s has Scored %s" % (f, score))
            print('\n')

            pdfFileObj.close()

# Rank the CV's on the basis of Scoring
zs.sort(reverse=True)
print(zs)

程序的输出是:

cv2.pdf
Name: Danish Ejaz  
GPA: 3.7  
Skills: Python, Java  
Experience: 2.5 years 

Danish Ejaz has Scored 5.5


cv3.pdf
Name: Abdullah  
GPA: 3.2  
Skills: Laravel, Java  
Experience: 2 years 

Abdullah has Scored 4


cv5.pdf
Name: M. Abrar Hussain  
GPA: 3.5  
Skills: Python, Laravel  
Experience: 3 years 

M. Abrar Hussain has Scored 7


[7, 5.5, 4]

Process finished with exit code 0

倒数第二行是计分后的结果。在这个结果中,它只是给了我们得分号,我也可以在结果中调用文件名吗?如果是,请帮助我完成这个项目。

标签: pythoncall

解决方案


您只需要存储文件名并在最后将它们与分数一起打印:

# Create a dictionary with filenames as key and add the score as value
# Note that this might be an issue if you have irrelevant files in your directory
file_scores = dict.fromkeys(listdir('resume/'))

# call the Resume files by calling the folder name
for filename in file_scores:
    # Your scoring logic
    (...)

    # store score values in the dictionary
    file_scores[filename] = score

    (...)


# Remove items without value
file_scores = {k: v for k, v in file_scores.items() if v}

# Sort the dictionary based on score descending
file_scores = {k: v for k, v in sorted(file_scores.items(), key=lambda x: x[1], reverse=True)}

# Print the file and the score together
for filename, score in file_scores.items():
    if score:  # Ignore other files
        print(f"File {filename}: Score = {score}")

推荐阅读