似乎第一个for循环仅考虑第一个csv文件中的第一行,并且不会继续进行比较。我试图用多种方法重写它,但现在我完全迷失了,所以我求助于你们。
任何人都可以告诉我是否像我在这里那样写代码时出错:
with open("./products.csv", mode="r") as products_list1:
with open("./products2.csv", mode="r") as products_list2:
with open("./results.csv", mode="a") as results:
for i in products_list1:
for j in products_list2:
jaccard = get_jaccard_sim(i, j)
if jaccard >= 0:
results.writelines(i+","+j+"\n"+","+str(jaccard))
答案 0 :(得分:1)
在打开任何for
循环之前,您应该执行以下操作:
pdt_list1 = products_list1.readlines()
pdt_list2 = products_list2.readlines()
并在pdt_list1
和pdt_list2
上执行操作。
这将解决问题!祝你好运!
答案 1 :(得分:1)
您可以尝试
with open("./products.csv", mode="r") as products_list1:
lines1 = products_list1.readlines()
with open("./products2.csv", mode="r") as products_list2:
lines2 = products_list2.readlines()
with open("./results.csv", mode="a") as results:
for i in lines1:
for j in lines2:
jaccard = get_jaccard_sim(i, j)
if jaccard >= 0:
results.writelines(i + "," + j + "\n" + "," + str(jaccard))