0
votes

Here is a part of output of our program. In this output i want to extract "M. Abrar Hussain" by calling its first index part "Name:" and for rest of the output part, so i want the solution for this if it is possible.Whoosh Library is used for indexing in python so this library might help.

['Name: M. Abrar Hussain  ', 'GPA: 3.5  ', 'Skills: Python, Laravel  ', 'Experience: 3 years ']
4
This is unclear, what is the expected output? and what have you tried to archive it?Guy
i am working on a project CV Screening using python. so in Resume i want to extract the name of the applicant by calling just "Name:" part. In this output i used a simple resume with only Name, GPA, Skills and Experience and store in an array by breaking the lines. means index[0] is name index[1] is GPA and so on. so all i want is that by calling the first in the index i want the very next part in the output.Abrar Hussain

4 Answers

1
votes

You can turn this into a dictionary like this:

split_lst = [i.split(': ',1 ) for i in lst]
d = { a : b.rstrip() for a,b in split_lst }

The dictionary look like this:

{'Name': 'M. Abrar Hussain',
'GPA': '3.5',
'Skills': 'Python, Laravel',
'Experience': '3 years'}

You can then use it like this:

d["Name"] 

to get "M. Abrar Hussain "

Notes:

  • Using split(': ',1) is important in case string contains more than one ': '
  • rstrip is used to remove trailing spaces from the strings.
0
votes

Get the index of ":" character and based on that slice the string and use strip function to eliminate the whitespaces

candidate = ['Name: M. Abrar Hussain  ', 'GPA: 3.5  ', 'Skills: Python, Laravel  ', 'Experience: 3 years ']

record = {}

for field in candidate:
    index = field.index(':')
    field_name = field[:index].strip()
    value=field[index+1:].strip()
    record[field_name]=value

field_input = input("Enter the field name")
print(record[field_name])

Output

M. Abrar Hussain
0
votes

If you want it to print out the whole field, using

applicant=['Name: M. Abrar Hussain  ', 'GPA: 3.5  ', 'Skills: Python, Laravel  ', 'Experience: 3 years ']

print(applicant[0])

will print out the name Name: M. Abrar Hussain

If you want to get just the actual value (by I assume the content after the :), you can use the split method print out the field

applicant=['Name: M. Abrar Hussain  ', 'GPA: 3.5  ', 'Skills: Python, Laravel  ', 'Experience: 3 years ']

def GetField(index):
    print(applicant[index].split(':')[1])

GetField(0)

# Output:
# M. Abrar Hussain

You should consider a class or a dictionary over an array though, because arrays don't have any way to identify a field besides an index.

0
votes
my_list = ['Name: M. Abrar Hussain  ', 'GPA: 3.5  ', 'Skills: Python, Laravel  ', 'Experience: 3 years ']
for item in my_list:
    x=item.split(':')[0].strip()
    y=item.split(':')[1].strip()
    my_map[x]=y
print(my_map)
print(my_map['Name'])

The above code should result in below as expected:

{'Experience': '3 years', 'Name': 'M. Abrar Hussain', 'GPA': '3.5', 'Skills': 'Python, Laravel'}
M. Abrar Hussain

You can store the above output in a dictionary or another list as you like.