Student1001
Student1001

Reputation: 111

csv output is incomplete

I have created two dictionaries named links and title with similar keys but different values. I am trying to write the an excel file using these dictionaries. However, while my links and title dictionaries have 250 keys, I am only able to write 244 lines for the csv file. Is there a way to get all 250 of them onto the csv file? P.S. I have ran:

print key,val,i #under the line p.append(i)

and the output runs till the 250th key, but not the csv file.

w = csv.writer(open("output.csv", "w"))


for key, val in links.items():

    p=[]
    p.append(key)
    p.append(val)
        for i in title[key]:
        p.append(i)
    w.writerow(p)

Upvotes: 0

Views: 718

Answers (1)

polku
polku

Reputation: 1590

I think it's because you don't close your file. Calling writerow doesn't mean your file is written immediately. By calling close you ensure any pending writings will be executed.

So close your file, or use the with open(...) as construct.

edit:

 with open("output.csv", "w") as f:
     w = csv.writer(f)

Upvotes: 1

Related Questions