Ask Your Question
0

reading, computing, writing

asked 2012-08-13 01:52:32 +0200

reddwarf2956 gravatar image

updated 2015-01-14 10:42:33 +0200

FrédéricC gravatar image

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

2 Answers

Sort by » oldest newest most voted
2

answered 2012-08-13 02:37:40 +0200

benjaminfjones gravatar image

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/

edit flag offensive delete link more
2

answered 2012-08-13 16:16:41 +0200

calc314 gravatar image

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])
edit flag offensive delete link 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.

reddwarf2956 gravatar imagereddwarf2956 ( 2012-08-13 17:25:38 +0200 )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?

reddwarf2956 gravatar imagereddwarf2956 ( 2012-08-13 17:31:52 +0200 )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)?

reddwarf2956 gravatar imagereddwarf2956 ( 2012-08-13 17:43:11 +0200 )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)

calc314 gravatar imagecalc314 ( 2012-08-13 19:10:54 +0200 )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

Stats

Asked: 2012-08-13 01:52:32 +0200

Seen: 3,381 times

Last updated: Aug 13 '12