1

I have a code which takes a user's input which is supposed to be a first and last name. My code then takes that input and searches a csv for projects and hours that the input name is associated to. The problem I'm running into is if I misspell or don't capitalize the first letter of each name the code will not work. Is there a function or a way out there where if the user inputs the first 3 letters right or something similar the code will automatically assume the correct spelling and change the input accordingly? I'm a very novice coder so any help would be great.

Here's my code for reference:

import csv

# I've used full name to avoid duplicate first names in report 
full_name = input('Enter your full name: ')

with open('Report1.csv') as csvfile:
    hour_summation = {}
    read_csv = csv.reader(csvfile, delimiter=',')
    for row in read_csv:
            if ' '.join((row[0], row[1])) == full_name.strip():
                hour_summation[row[2]] = hour_summation.get(row[2], 0) + int(float(row[3]))
print('This is {} full hours report:'.format(full_name))
for k, v in hour_summation.items():
    print(k + ': ' + str(v) + ' hours')
stevenmiller
  • 79
  • 10
  • 1
    If the number of possible user names isn't too large, then create a list of them, normalize all by calling `lower()` on it (same normalization for a name to search) and take a look at `difflib.SequenceMatcher`, especially `ratio()` – Michael Butscher Dec 05 '17 at 23:13
  • You could look at fuzzy matching. https://marcobonzanini.com/2015/02/25/fuzzy-string-matching-in-python/ – SuperStew Dec 05 '17 at 23:14
  • make all the strings lowercase before comparison should make things a lot easier, e.g. `full_name = input('Enter your full name: ').strip().lower()`, `' '.join((row[0], row[1])).lower()`, etc. – chickity china chinese chicken Dec 05 '17 at 23:22
  • Possible Dupliate of [Find the similarity percent between two strings](https://stackoverflow.com/q/17388213/2823755) – wwii Dec 05 '17 at 23:26

1 Answers1

1

You can use str.lower on both the user input and the value from the csv to ignore case.

You can use str.startswith to match the user input against the beginning of a value from the csv. You could also use in to detemrine if the user input is in a value from the csv.

Galen
  • 1,307
  • 8
  • 15