python 输入的拼写检查

Spell check for python input

我有一个代码可以接受用户的输入,该输入应该是名字和姓氏。然后,我的代码采用该输入并在 csv 中搜索输入名称关联的项目和时间。我 运行 遇到的问题是,如果我拼错或没有将每个名字的首字母大写,代码将无法工作。是否有一种功能或出路,如果用户正确输入前 3 个字母或类似的东西,代码将自动假定正确的拼写并相应地更改输入?我是一个非常新手的编码员,所以任何帮助都会很棒。

这是我的代码供参考:

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')

您可以对用户输入和 csv 中的值使用 str.lower 以忽略大小写。

您可以使用 str.startswith to match the user input against the beginning of a value from the csv. You could also use in 来确定用户输入的值是否来自 csv。