Ask Your Question

Writing data to file

asked 2011-09-19 16:01:20 -0600

G-Sage gravatar image

This is all in the Sage Notebook:

I want to store some data in a file, Excel or Access or something, because that's what I know. I am using statements like o.write("something" + ", " + "something" + "\r") and I'm doing this in a loop. I'm writing it to a txt file. So, Sage does its thing and there's the little "stuff.txt" link I can click on afterward. I click on it and it looks just how I want it. Then, using Firefox 6.whatever is out these days, I do file save as and save it as stuff.txt. I open it and there are no line breaks at all and the data is pretty much useless. I also tried "\n" and same thing.

I found a sort of solution, because I could use "stuff.csv" instead and it mostly works right. The problem there is, I have data which has commas in it. So, I was hoping to do a .txt file using tab delimited so the commas in the data didn't cause problems. But, the above problem for .txt files exists and I don't know how to fix it.

Oh, also just copying and pasting the data from the file Sage creates (before saving it using Firefox) didn't work today when I had nearly 300,000 lines of data. Excel/Access have the "[Not responding]" message, even though Excel and Access can easily handle 300,000 lines of data. Perhaps it is the clipboard is not big enough for that? I don't know.

Any ideas? Thanks for your help!

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2011-09-19 20:16:17 -0600

parzan gravatar image

If you're on a windows machine perhaps "\r\n" will work as end-of-line. See about this.

edit flag offensive delete link more


@parzan That worked perfectly. Thanks! This allowed me to use tab delimiters and import everything into Excel... well I haven't tried it with 300,000 lines of data yet. Let me do that. Yes, it worked perfectly. That was what I needed. Thanks so much.

G-Sage gravatar imageG-Sage ( 2011-09-20 03:03:48 -0600 )edit

You're welcome!

parzan gravatar imageparzan ( 2011-09-20 06:48:01 -0600 )edit

answered 2011-09-19 16:20:03 -0600

niles gravatar image

I'm assuming you're using something like the following to determine what file you write to?

sage: o = open('stuff.txt','w')
sage: o.write(str(f))

If you specify a more complete path for the output file (something like "~/stuff.txt"), then you won't have to save the file through your web browser, you can just open it directly from your filesystem in the location you specify. Does this resolve the problem?

(Note: you can do this in the other case too, you just have to find out what temp directory the notebook is saving your files in when you don't specify a complete path. But I think my suggestion is easier.)

edit flag offensive delete link more


@niles Yes, that is how I opened the file for writing. As far as writing it to a certain directory... I am on a Sage server... so I might be able to write it to the hard drive of the server but then I'd have to figure out how to get it off :) It is good to know though, so thanks for the comment. I will look into this.

G-Sage gravatar imageG-Sage ( 2011-09-20 03:05:16 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2011-09-19 16:01:20 -0600

Seen: 4,093 times

Last updated: Sep 19 '11