Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I found an answer in Finch's book. First a quote:

“We used a module called os from the Python standard library module to help us write code that can run on multiple platforms. A text file must have a special character to denote the end of each line in the file. Unfortunately, for historical reasons, each family of operating systems (Mac, Windows, and UNIX) uses a different end-of-line character. The os module has a constant called linesep that contains the correct character for the platform that the code is run on. We used the statement import os to make the module available, and accessed the constant using the syntax os.linesep. We also used the function os.path.join to join the path to the file name with the correct character for the current operating system.”

Excerpt From: Craig Finch. “Sage Beginner's Guide.”

Example using a file named "File2.txt" containing a single text character, '1':

import os path='/Users/barrybrent/.sage/sage_notebook.sagenb/home/__store__/2/21/212/2123/admin/19/data/' fileName='File2.txt' times = [] text_file = open(os.path.join(path, fileName), 'r') line = text_file.readline()

(Comment: is just a character string. To convert it a Sage object useful in computations:)

elements=line.split(',') times.append(float(elements[0].strip()))

(Comment: evaluate:)

times[0]

(Comment: Sage says"1.0". Now can we do arithmetic with times[0]?)

times[0]+1

Sage says "2.0"

I found an answer in Finch's book. First a quote:

“We used a module called os from the Python standard library module to help us write code that can run on multiple platforms. A text file must have a special character to denote the end of each line in the file. Unfortunately, for historical reasons, each family of operating systems (Mac, Windows, and UNIX) uses a different end-of-line character. The os module has a constant called linesep that contains the correct character for the platform that the code is run on. We used the statement import os to make the module available, and accessed the constant using the syntax os.linesep. We also used the function os.path.join to join the path to the file name with the correct character for the current operating system.”

Excerpt From: Craig Finch. “Sage Beginner's Guide.”

Example using a file named "File2.txt" containing a single text character, '1':

import os path='/Users/barrybrent/.sage/sage_notebook.sagenb/home/__store__/2/21/212/2123/admin/19/data/' fileName='File2.txt' times = [] text_file = open(os.path.join(path, fileName), 'r') line = text_file.readline()

(Comment: is just a character string. To convert it a Sage object useful in computations:)

elements=line.split(',') times.append(float(elements[0].strip()))

(Comment: evaluate:)

times[0]

(Comment: Sage says"1.0". Now can we do arithmetic with times[0]?)

times[0]+1

Sage says "2.0"

click to hide/show revision 3
No.3 Revision

I found an answer in Finch's book. First a quote:

“We used a module called os from the Python standard library module to help us write code that can run on multiple platforms. A text file must have a special character to denote the end of each line in the file. Unfortunately, for historical reasons, each family of operating systems (Mac, Windows, and UNIX) uses a different end-of-line character. The os module has a constant called linesep that contains the correct character for the platform that the code is run on. We used the statement import os to make the module available, and accessed the constant using the syntax os.linesep. We also used the function os.path.join to join the path to the file name with the correct character for the current operating system.”

Excerpt From: Craig Finch. “Sage Beginner's Guide.”

Example using a file named "File2.txt" containing a single text character, '1':

import os
path='/Users/barrybrent/.sage/sage_notebook.sagenb/home/__store__/2/21/212/2123/admin/19/data/'
fileName='File2.txt'
times = []
text_file = open(os.path.join(path, fileName), 'r')
line = text_file.readline()

text_file.readline()

(Comment: is just a character string. To convert it a Sage object useful in computations:)

elements=line.split(',')
times.append(float(elements[0].strip()))

times.append(float(elements[0].strip()))

(Comment: evaluate:)

times[0]

times[0]

(Comment: Sage says"1.0". Now can we do arithmetic with times[0]?)

times[0]+1

times[0]+1

Sage says "2.0"