I have the following code,

import csv
w=csv.writer(file('newfile.csv','wb'),dialect='excel')
some_values=[[1,2,3],['A','B','C'],[4,'"5"','ab,c']]
w.writerows(some_values)

When I run it on my computer (a mac), newfile.csv is created. However, when I open newfile.csv, it is empty. Any ideas why?

Recommended Answers

All 3 Replies

It works perfectly on my Mac as follows:
Start python
run the script
Stop python
examine the file:

1133% cat newfile.csv
1,2,3
A,B,C
4,"""5""","ab,c"
1134%

What you have not done is flush() or close() the open file. If you remain in the python interpreter while looking, you will not see the output until the file is flushed or closed.

Thank you that makes sense. I have been trying to close it, but can't seem to get that syntax right. Can you tell me how? Thanks again.

I was able to close the file with the following code,

import csv
result=open('newfile.csv','wb')
writer=csv.writer(result,dialect='excel')
headings=
writer.writerow(headings)
result.close()

Thanks again for helping a newbie.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.