我尝试按如下方式导入 training.txt 数据。
def readTrainingData(training):
trainingData=[]
with open(training.txt) as f:
for line in f:
a1, a2 = line.strip().split()
trainingData.append((a1, a2))
return trainingData
之后,我尝试使用 traingdata 来测量一些分数,如下所示:
for pair in trainingData:
linkScores[pair[0]+''+pair[1]]= computeProximityScore(pair[0],pair[1],'Jaccard',neighbors)
但它给出了错误
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-17-2532640f4771> in <module>
----> 1 trainingData
NameError: name 'trainingData' is not defined
有人可以帮我吗?
谢谢
答案 0 :(得分:1)
当您将变量 training
传递给函数时,我不明白您尝试了什么。
但是当你打开一个文件时,你需要这样做:
```with open("file_name.txt") as f:```
此外,您无法在函数外部访问变量 trainingData
。
我更新了你的代码(我希望它是你想要的):
Main(或您运行该函数的任何其他地方):
trainingData = readTrainingData("training.txt")
# The rest of your code.
您的职能:
def readTrainingData(training):
trainingData = []
with open(training) as f:
for line in f:
a1, a2 = line.strip().split()
trainingData.append((a1, a2))
return trainingData