简体   繁体   中英

writing to .csv file in python not producing expected results

I have been experimenting with writing to a csv file lately and stumbled across a problem. I have a for loop running through a list of items, and would like to rewrite this list in a.csv file, with each element of the list being in a new row. So, I structured the code as follows.

import csv 

groceries = ["banana", "apple", "orange", "orange", "milk", "orange", 
         "banana"]

file_name = "an_example_file_here"

banana_count = 0
apple_count = 0
orange_count = 0 
milk_count = 0

for i in groceries :
    w = csv.writer(open(file_name + ".csv", "w")) #writing to .csv
    if i == "banana":
        banana_count += 1
        w.writerow(["banana"])
    elif i == "apple":
        apple_count += 1
        w.writerow(["apple"])
    elif i == "orange":
        orange_count += 1
        w.writerow(["orange"])
    elif i == "milk":
        milk_count +=1 
        w.writerow(["milk"])

w.writerow(["banana_count = ", str(banana_count)],)
w.writerow(["apple_count = ", str(apple_count)],)
w.writerow(["orange_count = ", str(orange_count)],)
w.writerow(["milk_count = ", str(milk_count)],)     

I expected to get a csv file looking something like: banana, apple, orange, orange, milk, orange, banana, banana_count = 2, apple_count = 1, orange_count = 3, milk_count = 1

But, the actual file I am getting looks like: banana, banana_count = 2, apple_count = 1, orange_count = 3, milk_count = 1

I am not sure why, any help appreciated. Thank you!

You are overwriting your file at each iteration because you're using "w" as a parameter to the open function. try using "a" which means append to the file rather "w" which means write to a new file.

It seems you open the output file for each item of the list. The writing mode 'w' overwrite the file you've created for each previous item.

Try to open the file before the for loop.

When you write:

[...]
for i in groceries :
    w = csv.writer(open(file_name + ".csv", "w")) #writing to .csv
[...]

You are assigning the variable w to a new instance of csv's writer instance for each item in the groceries list.

So, each writer is starting from a blank file and overwriting what was written in the previous iteration. That's why your file is only getting the last element's csv output; because it is the only one without an iteration that follows it that will overwrite it.

You should initialize w before the loop.

the "w" must be set to "a" for append in w = csv.writer(open(file_name + ".csv", "w")) #writing to.csv

Also in the for loop it's not needed to write the file every time. just keep them in the end. note that it now also prints 5 rows. if this is not what you want put it in 1 statement in the end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM