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')
答案 0 :(得分:1)
您可以对用户输入和csv中的值使用str.lower
来忽略大小写。
您可以使用str.startswith
将用户输入与csv中值的开头相匹配。如果用户输入来自csv的值,您也可以使用in
来确定。