0

It looks like the first for loop only considers the first line in the first csv file and does not continue comparing. I have tried to rewrite it a bunch of ways but now im totally lost so im turning to you guys.

Can anyone tell me if there is anything wrong writing the code as i did here:

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))
4
  • You need to provide more information about what you want to accomplish. Commented Mar 1, 2019 at 12:31
  • Files aren't lists and must be re-opened each time before iterating over them. Commented Mar 1, 2019 at 12:31
  • for i in products_list1: should read for i in products_list1.readlines(): to get an actual list to loop over Commented Mar 1, 2019 at 12:31
  • Thanks for the input. It works with .readlines() Commented Mar 1, 2019 at 12:52

2 Answers 2

1

you can try this

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))
Sign up to request clarification or add additional context in comments.

1 Comment

similar to what @Satya had mentioned here:stackoverflow.com/a/54944794/2987755
1

You should do something like this before opening any for loops:

pdt_list1 = products_list1.readlines()
pdt_list2 = products_list2.readlines()

and perform your operations on pdt_list1 and pdt_list2.

This will fix the issue! Good Luck!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.