Hi all,I've had trouble with this for a while.If I write the following in to a notebook cell:------import csvoutput=csv.writer(open('test.csv','wb'))data=[[1,2],[3,4],[5,6]]for r in data:output.writerow(r)-----
--
To post to this group, send email to sage-s...@googlegroups.com
To unsubscribe from this group, send email to sage-support...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-support
URL: http://www.sagemath.org
Vince
To unsubscribe from this group, send email to sage-support+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-support
URL: http://www.sagemath.org
I think you should close that. The following works fine in the
notebook. It makes no sense to close the file -- instead, you have
to delete the csv writer object, which flushes it to the file.
import csv
output=csv.writer(open('test.csv','wb'))
data=[[1,2],[3,4],[5,6]]
for r in data:
output.writerow(r)
del output
>>> sage-support...@googlegroups.com
>>> For more options, visit this group at
>>> http://groups.google.com/group/sage-support
>>> URL: http://www.sagemath.org
>>
>>
>>
>>
>> --
>> Dr Vincent Knight
>> Cardiff School of Mathematics
>> Senghennydd Road,
>> Cardiff
>> CF24 4AG
>> (+44) 29 2087 5548
>> www.vincent-knight.com
>> @drvinceknight
>> Skype: drvinceknight
>>
>
--
William Stein
Professor of Mathematics
University of Washington
http://wstein.org
This isn't guaranteed to flush it to the file, though, because del
only deletes the name. That it results in a flush is entirely a
cpython implementation detail because there are no live references to
it. Looking at the test.csv file after `del output`:
mcneil@ubuntu:~/coding$ python csvflush.py
['1,2\r\n', '3,4\r\n', '5,6\r\n']
mcneil@ubuntu:~/coding$ pypy csvflush.py
[]
Or more specifically in cpython:
import csv
fp = open("test.csv", "w")
output=csv.writer(fp)
data=[[1,2],[3,4],[5,6]]
for r in data:
output.writerow(r)
print(open('test.csv').readlines())
del output
print(open('test.csv').readlines())
del fp
print(open('test.csv').readlines())
produces
[]
[]
['1,2\r\n', '3,4\r\n', '5,6\r\n']
where nothing happens until the last reference to the *file* is lost,
which in cpython gets it closed.
I'd either call .flush() and/or .close() explicitly or wrap it in a
with block so that I don't have to remember.
Doug
On Sun, Mar 25, 2012 at 9:06 AM, Dima Pasechnik <dim...@gmail.com> wrote:
> OK, thanks for letting su know. It looks like a genuine bug, indeed.
> The issue is now reported to sagenb folks as
> https://github.com/sagemath/sagenb/issues/48I think you should close that. The following works fine in the
notebook. It makes no sense to close the file -- instead, you have
to delete the csv writer object, which flushes it to the file.import csv
output=csv.writer(open('test.csv','wb'))
data=[[1,2],[3,4],[5,6]]
for r in data:
output.writerow(r)del output
>>> For more options, visit this group at
>>> http://groups.google.com/group/sage-support
>>> URL: http://www.sagemath.org
>>
>>
>>
>>
>> --
>> Dr Vincent Knight
>> Cardiff School of Mathematics
>> Senghennydd Road,
>> Cardiff
>> CF24 4AG
>> (+44) 29 2087 5548
>> www.vincent-knight.com
>> @drvinceknight
>> Skype: drvinceknight
>>
>
To unsubscribe from this group, send email to sage-support...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-support
URL: http://www.sagemath.org
I wouldn't use the del method, even if it may work, just because it
looks so odd compared to usual file paradigms.
Searching for "cvs writer python empty file" on the net gives lots of
hits talking about this problem, and the main conclusion seems to be
that you need to close the file (or even better, use 'with' so the file
is automatically closed).
http://stackoverflow.com/questions/3976711/csvwriter-not-saving-data-to-file-why
>
> Another question is why this is not needed at sage prompt. Different
> behaviour in shell and in nb is never good, IMHO.
When you exit Sage (to look at the file), the files are automatically
closed (IIRC). In the notebook, the Sage session keeps running for the
next cell, so the difference seems to be natural.
Jason
--
To post to this group, send email to sage-s...@googlegroups.com
To unsubscribe from this group, send email to sage-support+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-support
URL: http://www.sagemath.org
import csv
o= open('test.csv','wb')
output=csv.writer(o)
a = 100
p = list(str(n(pi, digits = a)))
p.remove('.')
p = [int(i) for i in p]
for r in p: output.writerow(r)
del o
Error:
------------------------------------------------------------
File "<ipython console>", line 2
del o
^
SyntaxError: invalid syntax
also the same error if I use o.close()
I think I am not closing the for loop correctly? Am I correct?
for r in p:
output.writerow(r)
del o
Python is indentation sensitive and uses it to determine where the for loop stops.