【问题标题】:How to print the file name by calling a function name in the file in python如何通过在python中调用文件中的函数名来打印文件名
【发布时间】:2021-08-20 09:30:41
【问题描述】:

这是一个基于简历筛选的程序。在这个程序中,我为每个 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

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

【问题讨论】:

  • 您想在某些查看器中打开最好的 pdf 文件吗?
  • 你能发布你期望的输出吗?
  • @RomanPavelka 是的,评分基于经验、技能和 GPA。
  • @ignoring_gravity [7, 5.5, 4] 这是程序的输出,我想要这样的文件名 [7 cv5.pdf, 5.5 cv2.pdf, 4 cv3.pdf]

标签: python call


【解决方案1】:

恕我直言,正确的方法是定义一个类,最小的变体是

class Candidate:
    def __init__(self, name, score, filename):
        self.name = name
        self.score = score
        self.filename = filename

    def __gt__(self, other):
        return self.score > other.score

    def __str__(self):
       return f'Candidate{self.name, self.filename, self.score}' 

    def __repr__(self):
       return self.__str__()

把它放在你的主要for 循环之前。然后代替

            tt = zs.append(score)

            tt = zs.append(Candidate(f, score, filename))

否则它应该工作相同。以下是一些解释性用法:

class Candidate:
    def __init__(self, name, score, filename):
        self.name = name
        self.score = score
        self.filename = filename

    def __gt__(self, other):
        return self.score > other.score

    def __str__(self):
       return f'Candidate{self.name, self.filename, self.score}'

    def __repr__(self):
       return self.__str__()

# __init__ allows this
a = Candidate("Arnold", 10, "arnold.pdf")
b = Candidate("Betty", 11, "betty.pdf")

# __gt__ allows this
print(a < b)
print(a > b)

# __str__ allows this
print(a)

# __repr__ allows human-readable this
print([a, b])

# __repr__ and __gt__ allows human-readable this
print(sorted([b, a]))

这会打印出来

True
False
Candidate('Arnold', 'arnold.pdf', 10)
[Candidate('Arnold', 'arnold.pdf', 10), Candidate('Betty', 'betty.pdf', 11)]
[Candidate('Arnold', 'arnold.pdf', 10), Candidate('Betty', 'betty.pdf', 11)]

【讨论】:

  • 输出是:“ [<__main__.candidate object at>, <__main__.candidate object at>, <__main__.candidate object at>]”
  • @AbrarHussain 是的,对,我们还需要__repr__(self) 函数,我现在已经修复了我的帖子
  • 是的,没错,但有一件事是分数不是整数值,而是浮点值。在此输出中,它只显示一个整数。
  • 好的,现在它应该适用于任何可比较的分数......
  • 不客气!好吧,我也学到了一些东西:)
【解决方案2】:

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

# 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}")

【讨论】:

  • 这不起作用,因为它破坏了文件名和分数之间的链接。 zs 与文件的顺序不同。
  • @CHecht 啊,你完全正确!我会解决的。
  • @CHecht 我想我修好了 :)
  • @Rafael-WO 它给了我错误:“ Traceback(最近一次通话最后一次):文件“C:\ Users \ M。 Abrar Hussain\Desktop\cv\difflib.py",第 108 行,在 file_scores = {k: v for k, v in sorted(file_scores.items(), key=lambda x: x[1], reverse= True)} TypeError: 'int' 和 'NoneType' 的实例之间不支持 '
  • @AbrarHussain 我添加了一个额外的行来修复这个错误。您基本上必须首先删除所有值为None 的项目。
【解决方案3】:

实现您想要的最简单的方法是使用表结构。鉴于您在每个结果中都有相似的字段,例如,您可以创建一个 pd.DataFrame 并填写所有值,然后使用 sort_values 并选择分数列。

当然还有其他替代方案,您可以使用 np.argsort 或类似名称,但 DataFrame 可能是实现您所追求的最简单的方法。

【讨论】:

    猜你喜欢
    • 2016-11-09
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-11
    • 1970-01-01
    • 2020-05-31
    • 1970-01-01
    相关资源
    最近更新 更多