我正在尝试查找大学名称的模糊字符串匹配项,并根据最接近的匹配项来自哪个列表,每次都将一定的分数(10,5,3)打印到csv。
data = [["MIT"], ["Stanford"], ...]
Data1 = ['MASSACHUSETTS INSTITUTE OF TECHNOLOGY (MIT)'], ['STANFORD UNIVERSITY'],...
到目前为止,我已经尝试过:
1 for uni in data:
2 hit = process.extractOne(str(uni[0]), data1, scorer = fuzz.token_set_ratio, score_cutoff = 90)
3 if float(hit[1]) < 100:
4 print("not found")
5 else:
print("Closest match for " + str(uni[0]) + " is " + str(hit[0]) " + "score: 10")
这时我得到第3行的TypeError: NoneType is unsubscriptable
我已经检查了变量的类型:
print(type(hit)) #I was getting tuple now NoneType...
print(len(hit)) # Was getting 2 now unsubscriptable
print(float(hit[1])) # 100
据我了解,当变量不是人们认为的类型时,就会出现此错误。任何想法如何解决这个问题?非常感谢
由于@inthevortex,我能够按如下所示完成代码:
for uni in data:
hit = process.extractOne(str(uni[0]), data10, scorer = fuzz.token_set_ratio, score_cutoff = 90)
try:
if float(hit[1]) >= 94:
with open(filename, mode='a', newline="") as csv_file:
fieldnames = ['bwbnr', 'uni_name', 'match', 'points']
writer = csv.DictWriter(csv_file, fieldnames=fieldnames, delimiter=';')
writer.writerow({'bwbnr': str(uni[0]), 'uni_name': str(uni[0]), 'match': str(hit), 'points': 10})
except:
hit1 = process.extractOne(str(uni[0]), data11, scorer = fuzz.token_set_ratio, score_cutoff = 90)
try:
if float(hit1[1]) >= 94:
with open(filename, mode='a', newline="") as csv_file:
fieldnames = ['bwbnr', 'uni_name', 'match', 'points']
writer = csv.DictWriter(csv_file, fieldnames=fieldnames, delimiter=';')
writer.writerow({'bwbnr': str(uni[0]), 'uni_name': str(uni[0]), 'match': str(hit1), 'points': 5})
...等等,直到最后一个。
答案 0 :(得分:0)
由于@inthevortex使用的try-除方法我完成的代码:
for uni in data:
hit = process.extractOne(str(uni[0]), data10, scorer = fuzz.token_set_ratio, score_cutoff = 90)
try:
if float(hit[1]) >= 94:
with open(filename, mode='a', newline="") as csv_file:
fieldnames = ['bwbnr', 'uni_name', 'match', 'points']
writer = csv.DictWriter(csv_file, fieldnames=fieldnames, delimiter=';')
writer.writerow({'bwbnr': str(uni[0]), 'uni_name': str(uni[0]), 'match': str(hit), 'points': 10})
except:
hit1 = process.extractOne(str(uni[0]), data11, scorer = fuzz.token_set_ratio, score_cutoff = 90)
try:
if float(hit1[1]) >= 94:
with open(filename, mode='a', newline="") as csv_file:
fieldnames = ['bwbnr', 'uni_name', 'match', 'points']
writer = csv.DictWriter(csv_file, fieldnames=fieldnames, delimiter=';')
writer.writerow({'bwbnr': str(uni[0]), 'uni_name': str(uni[0]), 'match': str(hit1), 'points': 5})
一直到我想与之比较的最后一个列表,再次使用try-except!