我正在尝试运行一个 python 程序,该程序可以从一个文件中运行一个字典,其中包含一个单词列表,每个单词都有一个分数和标准差。我的程序如下所示:
theFile = open('word-happiness.csv' , 'r')
theFile.close()
def make_happiness_table(filename):
'''make_happiness_table: string -> dict
creates a dictionary of happiness scores from the given file'''
with open(filename) as f:
d = dict( line.split(' ') for line in f)
return d
make_happiness_table("word-happiness.csv")
table = make_happiness_table("word-happiness.csv")
(score, stddev) = table['hunger']
print("the score for 'hunger' is %f" % score)
我的 .csv 文件格式为
word{TAB}score{TAB}standard_deviation
我正在尝试以这种方式创建字典。如何创建这样的字典,以便从函数中打印诸如“饥饿”之类的单词并获取其分数和标准偏差?