Exporting data from sage notebook

225 views
Skip to first unread message

Vincent Knight

unread,
Mar 25, 2012, 6:21:23 AM3/25/12
to sage-s...@googlegroups.com
Hi all,

I've had trouble with this for a while.

If I write the following in to a notebook cell:

------
import csv

output=csv.writer(open('test.csv','wb'))

data=[[1,2],[3,4],[5,6]]

for r in data:
    output.writerow(r)

-----


and evaluate, a hyperlink appears entitled 'test.csv', if I open that csv file it is empy. Checking the code in bash mode in sage (and python) does however work (and create the required csv file). 

Am I missing something to do with the notebook? 

Thanks,
Vince

PS I've tried this on the notebook running on my machine and sagenb as well...

--
Dr Vincent Knight
Cardiff School of Mathematics
Senghennydd Road,
Cardiff
CF24 4AG
(+44) 29 2087 5548
Skype: drvinceknight

Dima Pasechnik

unread,
Mar 25, 2012, 7:26:57 AM3/25/12
to sage-s...@googlegroups.com


On Sunday, 25 March 2012 18:21:23 UTC+8, Vincent Knight wrote:
Hi all,

I've had trouble with this for a while.

If I write the following in to a notebook cell:

------
import csv

output=csv.writer(open('test.​csv','wb'))

data=[[1,2],[3,4],[5,6]]

for r in data:
    output.writerow(r)

-----

Perhaps you should close() what what was open() ? I.e.

o= open('test.​csv','wb')
output=csv.writer(o)
# do the stuff....
o.close()

Vincent Knight

unread,
Mar 25, 2012, 8:05:43 AM3/25/12
to sage-s...@googlegroups.com
Thanks for the answer Dima,
I tried this:

-----

import csv

o=open('test.csv','wb')
output=csv.writer(o)

data=[[1,2],[3,4],[5,6]]

for r in data:
    output.writerow(r)
    
o.close()

----

I'm afraid that that doesn't work either (still output an empty csv file). Note that I have never had to use the close statement when running in python or bash mode so I can only assume that it's an issue with the notebook...

Vince

--
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

Dima Pasechnik

unread,
Mar 25, 2012, 9:06:55 AM3/25/12
to sage-s...@googlegroups.com, sage-n...@googlegroups.com
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/48
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

William Stein

unread,
Mar 25, 2012, 5:12:12 PM3/25/12
to sage-n...@googlegroups.com, sage-s...@googlegroups.com
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/48

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

D. S. McNeil

unread,
Mar 25, 2012, 7:31:33 PM3/25/12
to sage-s...@googlegroups.com
> 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.

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

Dima Pasechnik

unread,
Mar 25, 2012, 11:39:11 PM3/25/12
to sage-n...@googlegroups.com, sage-s...@googlegroups.com


On Monday, 26 March 2012 05:12:12 UTC+8, William Stein wrote:
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/48

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


wow, deleting does this?!  
I duly sought close() method for output, didn't find it...

Another question is why this is not needed at sage prompt. Different behaviour in shell and in nb is never good, IMHO.

>>> sage-support+unsubscribe@​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
>>
>

Vincent Knight

unread,
Mar 26, 2012, 4:10:18 AM3/26/12
to sage-s...@googlegroups.com, sage-n...@googlegroups.com
Thanks, using the del statement does work but I have to admit that it would be great if this was easier to find out (perhaps it is and I've just been looking in the wrong place).

Thanks,
Vince

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

Jason Grout

unread,
Mar 26, 2012, 9:22:03 AM3/26/12
to sage-s...@googlegroups.com
On 3/25/12 10:39 PM, Dima Pasechnik wrote:
> wow, *deleting* does this?!

> I duly sought close() method for output, didn't find it...

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

Vincent Knight

unread,
Mar 26, 2012, 9:31:12 AM3/26/12
to sage-s...@googlegroups.com
Thanks, I don't suppose it would be worth someone trying to implement a simpler import/export function? I'm a lot happier with the underlying python now so it's not too big a deal but for a beginner this could be a bit of a sticking point (it was for me).

Cheers,
Vince


--
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

Eric Kangas

unread,
Apr 4, 2012, 4:51:53 PM4/4/12
to sage-s...@googlegroups.com
I am using Cantor instead of the command line. Here is the issue I come up with when trying to save to a file.

Code:

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?

Maarten Derickx

unread,
Apr 6, 2012, 8:06:56 PM4/6/12
to sage-s...@googlegroups.com
Yes the last few lines should read:


for r in p:
output.writerow(r)
del o


Python is indentation sensitive and uses it to determine where the for loop stops.

Eric Kangas

unread,
Apr 12, 2012, 7:05:34 PM4/12/12
to sage-s...@googlegroups.com
However Cantor doesn't allow for indentations. when it comes to loops for sage.
Reply all
Reply to author
Forward
0 new messages