# Writing data to file

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 close merge delete

Sort by » oldest newest most voted

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.

( 2011-09-20 10:03:48 +0200 )edit

You're welcome!

( 2011-09-20 13:48:01 +0200 )edit

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

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.

( 2011-09-20 10:05:16 +0200 )edit