<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
On 2/21/2012 10:00 PM, Michael Lewis wrote:
<blockquote
cite="mid:CAE5MWfU5QpTBQz8wWbChiActf7pWHtBJSKEkHgV7csUqFp5aQA@mail.gmail.com"
type="cite">
<div dir="ltr">Hi everyone,
<div><br>
</div>
<div>I have some code where I import a file to use a module.
That module that I import</div>
</div>
</blockquote>
<blockquote
cite="mid:CAE5MWfU5QpTBQz8wWbChiActf7pWHtBJSKEkHgV7csUqFp5aQA@mail.gmail.com"
type="cite">
<div dir="ltr">
<div> takes text and a multiplier, checks for any numbers in
that text and will then multiply those numbers by the given
multiplier. The imported module is below. I am getting the
text from a file that I have which starts out as:</div>
<div><br>
</div>
<div>.5 lb. butter</div>
<div>1.75 Cups Graham Cracker Crumbs</div>
<div>2.0 Cups Powder Sugar</div>
<div>1.0 Cups Peanut Butter</div>
<div>2.0 Cups Semi-sweet Chocolate Chips</div>
<div><br>
</div>
<div>It seems that the .isdigit() function that I use doesn't
recognize the .5 as a number and therefore doesn't multiply
it. How can I get my code to recognize numbers such as .5,
1.75 as numbers?</div>
<div><br>
</div>
</div>
</blockquote>
Wow - the requirements just changed. Up tilll now we were dealing
with multiplying each digit. Now we have to parse out a string that
represents a number with decimal places! I hope that we don't raise
the oven temperature in the process.<br>
<br>
Is it true that the number to multiply is always at the beginning of
a line? If so that makes the job a lot easier.<br>
<br>
<blockquote
cite="mid:CAE5MWfU5QpTBQz8wWbChiActf7pWHtBJSKEkHgV7csUqFp5aQA@mail.gmail.com"
type="cite">
<div dir="ltr">
<div>Imported module:</div>
<div><br>
</div>
<div>
<div>def MultiplyText(text, multiplier):</div>
<div> '''Recieve a S & int. For digits in S, multiply
by multiplier and return updated S.'''</div>
<div> return ' '.join(str(float(num) * multiplier) if
num.isdigit() else num for num in text)</div>
<div><br>
</div>
<div>Module doing the importing/opening/reading/creating a new
file</div>
<div><br>
</div>
<div>
<div>
import Homework5_1 as multiply</div>
<div><br>
</div>
<div>def DoubleDigits(file_name):</div>
<div> '''Open file/read file/write new file that doubles
all int's in the</div>
<div> read file'''</div>
<div> try:</div>
<div> read_file = open(file_name)</div>
<div> except IOError:</div>
<div> return 'No such file.'</div>
<div> new_file_name = file_name + '2'</div>
<div> try:</div>
<div>
write_file = open(new_file_name, 'w')</div>
<div> except IOError:</div>
<div> read_file.close()</div>
<div> return 'No such file to write to.'</div>
<div> for line in read_file:</div>
<div> new_line = line.split()</div>
<div> new_line =
''.join(multiply.MultiplyText(new_line, 2))</div>
<div> write_file.write(new_line + '\n')</div>
<div> read_file.close()</div>
<div>
write_file.close()</div>
<div><br>
</div>
<div>def main():</div>
<div> DoubleDigits(raw_input('What file do you want to
open? '))</div>
<div><br>
</div>
<div><br>
</div>
<div>if '__name__' == '__main__':</div>
<div> print main()</div>
</div>
<div><br>
</div>
<div>Output that is written to my file:</div>
<div><br>
</div>
<div>
<div>Peanut Butter Bars</div>
<div><br>
</div>
<div>Ingredients</div>
<div><br>
</div>
<div>.5 lb. butter</div>
<div>1.75 Cups Graham Cracker Crumbs</div>
<div>4.0 Cups Powder Sugar</div>
<div>2.0 Cups Peanut Butter</div>
<div>4.0 Cups Semi-sweet Chocolate Chips</div>
<div><br>
</div>
<div>Melt butter. Add graham cracker crumbs,</div>
<div>peanut butter and sugar. Mix well and</div>
<div>pat into sheet pan. Cover with melted</div>
<div>chocolate. Refrigerate until semi-firm,</div>
<div>then cut into squares.</div>
</div>
<div><br>
</div>
-- <br>
<div dir="ltr">
<div>Michael J. Lewis</div>
</div>
<br>
</div>
</div>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
Tutor maillist - <a class="moz-txt-link-abbreviated" href="mailto:Tutor@python.org">Tutor@python.org</a>
To unsubscribe or change subscription options:
<a class="moz-txt-link-freetext" href="http://mail.python.org/mailman/listinfo/tutor">http://mail.python.org/mailman/listinfo/tutor</a>
</pre>
</blockquote>
<br>
<br>
<pre class="moz-signature" cols="72">--
Bob Gailer
919-636-4239
Chapel Hill NC</pre>
</body>
</html>