[Tutor] Outputting to text files
Daniel Yoo
dyoo@hkn.EECS.Berkeley.EDU
Sun, 20 Aug 2000 14:28:47 -0700 (PDT)
On Sun, 20 Aug 2000, isopod graphics wrote:
> calculations like slope calculaion etc., now I wanna save or output the
> results to a text file.How do I go about doing that?I know how to read from
> a text file and print it on the screen but I don't know how to save the
You can open up a file for writing into:
###
>>> out = open('output.txt', 'w')
###
That second argument means, "open this for writing". Analogously, you
can open a file for reading with the 'r' option.
From this, we can start doing write()'s using 'out', and things should be
ok.
###
>>> in = open('output.txt', 'r')
File "<stdin>", line 1
in = open('output.txt', 'r')
^
SyntaxError: invalid syntax
###
Whoops! That was silly of me -- 'in' is a special keyword. (for/in)
Um... let's try 'input'.
###
>>> input = open('output.txt', 'r')
>>> input.read()
'Bonobos are cute.'
###
> results to a new file. Another one (sorry about this very simple Algebra
> question) how do you compute the y-intercept if you were just given the
> coordinates of 2 points (x1,y1 and x2,y2)?
Ah, line equations! The y-intercept is the place where the line hits the
y-axis. At this point, x=0. All we need to do is write a function that
uses x as a parameter, and we should be ok.
From algebra, here's the generic function that covers lines:
y(x) --> mx + b
The problem is, it's too generic! What we need to do is find out what 'm'
and 'b' are. These are "constants", and every line corresponds to a
unique pair of 'm' and 'b'. Thankfully, we can do this, because we have
two points. Those two points correspond to these equations:
y_1 = m * x_1 + b # (Equation 1)
y_2 = m * x_2 + b # (Equation 2)
Algebra tells us that we can "solve" the equation at this point ("two
equations, two unknowns"). Remember, we are looking for the values of 'm'
and 'b'. We just need to choose which one we want to go after first, and
things will fall into place. Let's go after 'm'.
b = y_1 - m * x_1 # I'm isolating 'b' from equation 1
y_2 = m * x_2 + b # Repeating equation 2. I will now substitute.
y_2 = m * x_2 + y_1 - m * x_1 # Our goal is to go after 'm'
m * x_2 - m * x_1 = y_2 - y_1 # Just moving stuff around.
m = (y_2 - y_1) / (x_2 - x_1) # There's m!
Finally! Ok, now that we have 'm', it's almost trivial to get b: just
plug it in:
b = y_1 - m * x_1 # Repeating the isolation from
# equation 1. Substitute.
b = y_1 - (y_2 - y_1)/(x_2 - x_1) * x_1 # There's b!
Our original goal was to find the y-intercept. We now have the equation
of the line:
y(x) -> m*x + b
And the y intercept occurs when y is zero, so:
y(0) -> m*0 + b
y(0) -> b
That is, the 'y' intercept is 'b'. The python function to calculate y
intercepts, then, is:
def yIntercept(x_1, y_1, x_2, y_2):
x_1, y_1, x_2, y_2 = map(float, [x_1, y_1, x_2, y_2]) # tricky!
return y_1 - (y_2 - y_1)/(x_2 - x_1) * x_1
The tricky line just forces all of those values to be floating point. The
reason I'm doing this is because we don't want integer division --- we
want to work with an approximation to real-valued math. I'm just
guaranteeing that we're working with floating point, by doing the
conversions.
Let's test it out:
###
>>> yIntercept(0, 0, 1, 1) # Just to see if it works
0.0
>>> yIntercept(2, 3, 2, 4)
Traceback (innermost last):
File "<stdin>", line 1, in ?
File "<stdin>", line 3, in yIntercept
ZeroDivisionError: float division
>>> yIntercept(2, 3, 3, 4)
1.0
###
But because it's an approximation to real math, you might get weird values
sometimes --- be careful of floating point math. Ok, sorry about the
length of the message. ASCII text isn't an easy medium to work
mathematics with... *grin*