0

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?

2
Contributors
3
Replies
10
Views
7 Years
Discussion Span
Last Post by oaktrees
0

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.

0

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.

0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.