0

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!

0

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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.

Comments

0

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

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.