On Wed, Feb 22, 2012 at 4:35 PM, bob gailer <[email protected]> wrote: > On 2/21/2012 10:00 PM, Michael Lewis wrote: > > Hi everyone, > > I have some code where I import a file to use a module. That module that I > import > > 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: > > .5 lb. butter > 1.75 Cups Graham Cracker Crumbs > 2.0 Cups Powder Sugar > 1.0 Cups Peanut Butter > 2.0 Cups Semi-sweet Chocolate Chips > > 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? > > 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. > > 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. > > Imported module: > > def MultiplyText(text, multiplier): > '''Recieve a S & int. For digits in S, multiply by multiplier and return > updated S.''' > return ' '.join(str(float(num) * multiplier) if num.isdigit() else num > for num in text) > > Module doing the importing/opening/reading/creating a new file > > import Homework5_1 as multiply > > def DoubleDigits(file_name): > '''Open file/read file/write new file that doubles all int's in the > read file''' > try: > read_file = open(file_name) > except IOError: > return 'No such file.' > new_file_name = file_name + '2' > try: > write_file = open(new_file_name, 'w') > except IOError: > read_file.close() > return 'No such file to write to.' > for line in read_file: > new_line = line.split() > new_line = ''.join(multiply.MultiplyText(new_line, 2)) > write_file.write(new_line + '\n') > read_file.close() > write_file.close() > > def main(): > DoubleDigits(raw_input('What file do you want to open? ')) > > > if '__name__' == '__main__': > print main() > > Output that is written to my file: > > Peanut Butter Bars > > Ingredients > > .5 lb. butter > 1.75 Cups Graham Cracker Crumbs > 4.0 Cups Powder Sugar > 2.0 Cups Peanut Butter > 4.0 Cups Semi-sweet Chocolate Chips > > Melt butter. Add graham cracker crumbs, > peanut butter and sugar. Mix well and > pat into sheet pan. Cover with melted > chocolate. Refrigerate until semi-firm, > then cut into squares. > > -- > Michael J. Lewis > > > > _______________________________________________ > Tutor maillist - [email protected] > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > > > -- > Bob Gailer > 919-636-4239 > Chapel Hill NC > > > _______________________________________________ > Tutor maillist - [email protected] > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > If the first stuff in the line is always the number, and you capture the line in s then:
n = float(s.split()[0]) will give you the number. You should wrap this up with a try block so that it doesn't fail if it isn't a number split method separates on whitespace and puts each part of the string in a list. [0] takes the first of those list items. float converts to a float -- Joel Goldstick _______________________________________________ Tutor maillist - [email protected] To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
