Ask Your Question

# reading, computing, writing

OK, I just installed Sage for the first time and now I would like to figure out how to read and write a file as well as run 'functions' like primepi (count of prime numbers).

Axxxxxx is a sequence in the OEIS at oeis.org.

The data file has two columns "n, A104272(n)" with n > 3*10^6:

1 2 2 11 3 17 4 29 5 41 ... ...

After reading the data I need it take the difference of the A182873(n) = A104272(n+1) - A104272(n), find primepi(A104272(n+1)) and primepi(A104272(n)) and the difference of those two A190874(n) = primepi(A104272(n+1)) - primepi(A104272(n)). After that I would like to write the data back to a file.

I have been looking at the tutorial, but I have not found the section on reading data or anything like it.

edit retag close merge delete

## 2 answers

Sort by » oldest newest most voted

Sage builds on top of Python which provides pretty much any general purpose programming functionality. There are plenty of good tutorials on reading / writing data using Python, but you might find this one useful:

http://www.ibm.com/developerworks/opensource/library/os-python8/

more

Here is one collection of some simple python code.

To read a data file, the file is stored with the Sage notebook in a directory labelled DATA by Sage.

import csv
data =list( csv.reader(open(DATA+'infile.csv','rU')) )


The data is then in a list that you can manipulate.

Some code for writing a list named info that contains lists is:

writefile = csv.writer(open(DATA+'outfile.csv', 'w'))
for i in range(0,len(info)):
writefile.writerow(info[i])

more

## Comments

So is this reading/writing one row at a time? or the whole file? I realize that the manipulation is done different depending on which method is used. I think each of the sequences made will be written in a seperate file. So, I am not expecting any overwrite issues.

( 2012-08-13 10:25:38 -0500 )edit

I found information with prime_pi. It was not primepi. I see that you stated that the data file 'infile.csv' is "stored with the Sage notebook in a directory labelled DATA by Sage". Can it be placed elsewhere?

( 2012-08-13 10:31:52 -0500 )edit

Oh, yea. My data file is just a text file. I see you have the .csv extension does this matter on how the file is read or is a .csv file text (I know that I can use a different file name)?

( 2012-08-13 10:43:11 -0500 )edit

1) The code above reads the whole file and puts each line in a list within the list named data. 2) I think this depends on whether you are using the notebook interface. If so, then the data file is stored with the notebook file and I don't know if you want to change that. If you are using the command line interface, I think you can store the file wherever you want. 3) A .csv file is a plain text file in which data are separated by commas. There are end of line and end of file markers as well. Otherwise, that's it. So, this is a convenient format for storing and sharing data. You can change the name but should keep the same extension. I do recommend finding a good python reference. That helps greatly with any Sage work, but is even more important for what ...(more)

( 2012-08-13 12:10:54 -0500 )edit

## Your Answer

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

Add Answer

## Stats

Asked: 2012-08-12 18:52:32 -0500

Seen: 2,895 times

Last updated: Aug 13 '12