Google Groups

Re: [sage-notebook] Re: [sage-support] Re: Exporting data from sage notebook


Vincent Knight Mar 26, 2012 1:10 AM
Posted in group: sage-support
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

On 26 March 2012 04:39, Dima Pasechnik <dim...@gmail.com> wrote:


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.

>
>
> On Sunday, 25 March 2012 20:05:43 UTC+8, Vincent Knight wrote:
>>
>> 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
>>
>> On 25 March 2012 12:26, Dima Pasechnik <dim...@gmail.com> wrote:
>>>
>>>
>>>
>>> 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()
>>>
>>>
>>>>
>>>>
>>>> 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
>>>> www.vincent-knight.com
>>>> @drvinceknight
>>>> Skype: drvinceknight
>>>>
>>> --
>>> 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
>>
>>
>>
>>
>> --
>> 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

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



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