Re: [Tutor] reading complex data types from text file
okay so I figured my program out. I am posting the final version so if someone else is having problems with something like this it may benefit the community. (comments are included to help those who might not understand the code) ''' Program reads names of bowlers and their corresponding scores, then writes their names to a new text file with a description of either below average, average, above average or a perfect score ''' scores = {}# empty dictionary total = 0#initalize total to 0 value for line in open(bowlingscores.txt, r):# iterate through txt file with names and scores if line.strip().isdigit(): score = int(line)# convert score into int type scores[name] = score# add scores to dictionary total += score# add score to running total else: name = line.strip()# if the line isn't a digit name will be the key in the dictionary averageScore = total / len(scores)# get average of all scores fileOut = open(bowlingaverages.txt, w)# create a file to write names and scores to fileOut.write(Bowling Report\n + (- * 50) + \n)# header for name, score in scores.items():#iterate through each score in the dictionary to get an score value for each player if score == 300: score = \tPerfect score!\n scores[name] = score elif score averageScore: score = \tBelow average\n scores[name] = score elif score averageScore: score = \tAbove average!\n scores[name] = score else: score = \tAverage!\n scores[name] = score for items in scores.items():#iterate through the items in the dictionary and format them to the output file fileOut.write(%s%s\n % items) --- your output for this code should look like this inside the text file: *Bowling Report -- sueBelow average billAbove average! natBelow average tomPerfect score!* Thanks to everyone who helped me with this. On Sun, Jul 19, 2009 at 12:15 AM, Chris Castillo ctc...@gmail.com wrote: so could I also replace the score of each bowler (key value) in the dictionary with a new key such as below average or above average according to each if-elif-else structure and then write to a text file in the following format? Jim Above Average SueBelow Average BobPerfect score On Fri, Jul 17, 2009 at 12:48 PM, bob gailer bgai...@gmail.com wrote: Chris Castillo wrote: how would i go about adding the names to a dictionary as a key and the scores as a value in this code? # refactored for better use of Python, correct logic, and flow scores = {} # empty dictionary total = 0 for line in open(bowlingscores.txt, r): if line.strip().isdigit(): score = int(line) scores[name] = score total += score else: name = line.strip() averageScore = total / len(scores) fileOut = open(bowlingaverages.txt, w) fileOut.write(Bowling Report\n + (- * 50) + \n) for name, score in scores.items(): if score == 300: score = \tPerfect score! elif score averageScore: score = \tBelow average elif score averageScore: score = \tAbove average! else: score = \tAverage! print name, score -- Bob Gailer Chapel Hill NC 919-636-4239 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reading complex data types from text file
so could I also replace the score of each bowler (key value) in the dictionary with a new key such as below average or above average according to each if-elif-else structure and then write to a text file in the following format? Jim Above Average SueBelow Average BobPerfect score On Fri, Jul 17, 2009 at 12:48 PM, bob gailer bgai...@gmail.com wrote: Chris Castillo wrote: how would i go about adding the names to a dictionary as a key and the scores as a value in this code? # refactored for better use of Python, correct logic, and flow scores = {} # empty dictionary total = 0 for line in open(bowlingscores.txt, r): if line.strip().isdigit(): score = int(line) scores[name] = score total += score else: name = line.strip() averageScore = total / len(scores) fileOut = open(bowlingaverages.txt, w) fileOut.write(Bowling Report\n + (- * 50) + \n) for name, score in scores.items(): if score == 300: score = \tPerfect score! elif score averageScore: score = \tBelow average elif score averageScore: score = \tAbove average! else: score = \tAverage! print name, score -- Bob Gailer Chapel Hill NC 919-636-4239 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reading complex data types from text file
Chris Castillo wrote: how would i go about adding the names to a dictionary as a key and the scores as a value in this code? # refactored for better use of Python, correct logic, and flow scores = {} # empty dictionary total = 0 for line in open(bowlingscores.txt, r): if line.strip().isdigit(): score = int(line) scores[name] = score total += score else: name = line.strip() averageScore = total / len(scores) fileOut = open(bowlingaverages.txt, w) fileOut.write(Bowling Report\n + (- * 50) + \n) for name, score in scores.items(): if score == 300: score = \tPerfect score! elif score averageScore: score = \tBelow average elif score averageScore: score = \tAbove average! else: score = \tAverage! print name, score -- Bob Gailer Chapel Hill NC 919-636-4239 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reading complex data types from text file
Chris Castillo wrote: why does your 3rd and fourth lines have brackets? On Thu, Jul 16, 2009 at 1:08 AM, Christian Witts cwi...@compuscan.co.za mailto:cwi...@compuscan.co.za wrote: Chris Castillo wrote: I'm having some trouble reading multiple data types from a single text file. say I had a file with names and numbers: bob 100 sue 250 jim 300 I have a few problems. I know how to convert the lines into an integer but I don't know how to iterate through all the lines and just get the integers and store them or iterate through the lines and just get the names and store them. please help. ___ Tutor maillist - Tutor@python.org mailto:Tutor@python.org http://mail.python.org/mailman/listinfo/tutor You could do it with a list comprehension names = [] numbers = [] [numbers.append(int(line.strip())) if line.strip().isdigit() else names.append(line.strip()) for line in open('test.txt','rb') if line.strip()] [None, None, None, None, None, None] names, numbers (['bob', 'sue', 'jim'], [100, 250, 300]) The list comprehension would unfold to for line in open('test.txt', 'rb'): if line.strip(): if line.strip().isdigit(): numbers.append(line.strip()) else: names.append(line.strip()) And from there you can do what you like with the lists. -- Kind Regards, Christian Witts [numbers.append(int(line.strip())) if line.strip().isdigit() else names.append(line.strip()) for line in open('test.txt','rb') if line.strip()] [None, None, None, None, None, None] Are you referring to these lines ? If so, the reason is that for Python to recognize it as a list comprehension it needs to be wrapped in square brackets, if you were to use () instead to wrap around it it would become a generator expression (something which is incredibly powerful for larger amounts of data as it iterates when it needs to instead of pre-building everything. And the following line with the Nones on is because that is the output of the calls to .append. Normally you wouldn't see it in your application though. -- Kind Regards, Christian Witts ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reading complex data types from text file
Chris Castillo wrote: Oh okay. gotcha. so I have what I want basically. I just need to check to see if each number meets a certain criteria and output something like the following to a text file. Should I be going about this a different way or should I still use lists? bob below average sue above average jim perfect On Thu, Jul 16, 2009 at 4:12 AM, Christian Witts cwi...@compuscan.co.za mailto:cwi...@compuscan.co.za wrote: Chris Castillo wrote: why does your 3rd and fourth lines have brackets? On Thu, Jul 16, 2009 at 1:08 AM, Christian Witts cwi...@compuscan.co.za mailto:cwi...@compuscan.co.za mailto:cwi...@compuscan.co.za mailto:cwi...@compuscan.co.za wrote: Chris Castillo wrote: I'm having some trouble reading multiple data types from a single text file. say I had a file with names and numbers: bob 100 sue 250 jim 300 I have a few problems. I know how to convert the lines into an integer but I don't know how to iterate through all the lines and just get the integers and store them or iterate through the lines and just get the names and store them. please help. ___ Tutor maillist - Tutor@python.org mailto:Tutor@python.org mailto:Tutor@python.org mailto:Tutor@python.org http://mail.python.org/mailman/listinfo/tutor You could do it with a list comprehension names = [] numbers = [] [numbers.append(int(line.strip())) if line.strip().isdigit() else names.append(line.strip()) for line in open('test.txt','rb') if line.strip()] [None, None, None, None, None, None] names, numbers (['bob', 'sue', 'jim'], [100, 250, 300]) The list comprehension would unfold to for line in open('test.txt', 'rb'): if line.strip(): if line.strip().isdigit(): numbers.append(line.strip()) else: names.append(line.strip()) And from there you can do what you like with the lists. --Kind Regards, Christian Witts [numbers.append(int(line.strip())) if line.strip().isdigit() else names.append(line.strip()) for line in open('test.txt','rb') if line.strip()] [None, None, None, None, None, None] Are you referring to these lines ? If so, the reason is that for Python to recognize it as a list comprehension it needs to be wrapped in square brackets, if you were to use () instead to wrap around it it would become a generator expression (something which is incredibly powerful for larger amounts of data as it iterates when it needs to instead of pre-building everything. And the following line with the Nones on is because that is the output of the calls to .append. Normally you wouldn't see it in your application though. -- Kind Regards, Christian Witts Ok, I see what you want to do now. The best case for this would be to unfold it into a proper loop instead to maintain readability Name = None Number = None fOut = open('output.txt', 'wb') for line in open('test.txt', 'rb'): line = line.strip() if line: if line.isdigit(): Number = int(line) # Do whatever processing you want now else: Name = line if Name and Number: # Checks to see if Name, Number have values fOut.write('%s\t%s\r\n' % (Name, Number)) Name, Number = None, None # This is done so that you have to have a name and number # before writing to the file, it also then resets the # state of Name, Number to None before continuing fOut.close() What this will do is read in the file a line at a time, if the line is empty is continues to the next one without processing. If the line contains data it will check if it's numeric and if so populate the Number variable and if non-numeric populate the Name variable (gross assumption that it would indeed be a name and not something arbitrary like punctuation etc). Once both variables have been set that data would be written out to file for storing. -- Kind Regards, Christian Witts ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reading complex data types from text file
All lines that come back from a text file come back as strings. You can use string methods to detect the data like so: f = open('test.txt') lines = f.readlines() numbers = [] strings = [] for line in lines: if line.strip().isdigit(): numbers.append(int(line)) else: strings.append(line.strip()) print numbers print strings On Wed, Jul 15, 2009 at 1:55 PM, Chris Castillo ctc...@gmail.com wrote: I'm having some trouble reading multiple data types from a single text file. say I had a file with names and numbers: bob 100 sue 250 jim 300 I have a few problems. I know how to convert the lines into an integer but I don't know how to iterate through all the lines and just get the integers and store them or iterate through the lines and just get the names and store them. please help. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reading complex data types from text file
*so far I have this and the format is what i want:* -- # Set all necessary variables name = None fileOut = open('outputFile.txt', 'w') total = 0 averageScore = 0 numofScores = 0 score = 0 # Header for output file fileOut.write(Bowling Report\n + (- * 40) + \n) # Iterate line by line through file to get names and their corresponding scores for line in open('bowlingscores.txt', 'r'): line = line.strip() if line: if line.isdigit(): score = int(line) numofScores += 1 total += score averageScore = total / numofScores # Get average score # Decides where bowler stands compared to the average score if score averageScore: score = \tBelow average elif score averageScore and score != 300: score = \tAbove average! elif score == 300: score = \tPerfect score! else: name = line # Checks to see if name and score have values if name and score: fileOut.write('%s\t%s\r\n' % (name, score)) name, score = None, None fileOut.close() *the problem is that it's not comparing the first bowler's score to the average score and the output file looks like this: * Bowling Report David120 HectorPerfect score! MaryBelow average * is the logic in my if-elif statements wrong or is it just skipping the first bowler's score? * On Thu, Jul 16, 2009 at 8:26 AM, Glen Zangirolami digitalma...@gmail.comwrote: All lines that come back from a text file come back as strings. You can use string methods to detect the data like so: f = open('test.txt') lines = f.readlines() numbers = [] strings = [] for line in lines: if line.strip().isdigit(): numbers.append(int(line)) else: strings.append(line.strip()) print numbers print strings On Wed, Jul 15, 2009 at 1:55 PM, Chris Castillo ctc...@gmail.com wrote: I'm having some trouble reading multiple data types from a single text file. say I had a file with names and numbers: bob 100 sue 250 jim 300 I have a few problems. I know how to convert the lines into an integer but I don't know how to iterate through all the lines and just get the integers and store them or iterate through the lines and just get the names and store them. please help. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reading complex data types from text file
Chris Castillo wrote: *so far I have this and the format is what i want:* -- # Set all necessary variables name = None fileOut = open('outputFile.txt', 'w') total = 0 averageScore = 0 numofScores = 0 score = 0 # Header for output file fileOut.write(Bowling Report\n + (- * 40) + \n) # Iterate line by line through file to get names and their corresponding scores for line in open('bowlingscores.txt', 'r'): line = line.strip() if line: if line.isdigit(): score = int(line) numofScores += 1 total += score averageScore = total / numofScores # Get average score # Decides where bowler stands compared to the average score if score averageScore: score = \tBelow average elif score averageScore and score != 300: score = \tAbove average! elif score == 300: score = \tPerfect score! else: name = line # Checks to see if name and score have values if name and score: fileOut.write('%s\t%s\r\n' % (name, score)) name, score = None, None fileOut.close() *the problem is that it's not comparing the first bowler's score to the average score and the output file looks like this: * Bowling Report David120 HectorPerfect score! MaryBelow average * is the logic in my if-elif statements wrong or is it just skipping the first bowler's score? * You must process all the scores before computing averages. This means saving each player's score. Use a dictionary with the name as the key and the score as the value. Then go thru the dictionary to compute and report averages. -- Bob Gailer Chapel Hill NC 919-636-4239 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reading complex data types from text file
Chris Castillo wrote: I'm having some trouble reading multiple data types from a single text file. say I had a file with names and numbers: bob 100 sue 250 jim 300 I have a few problems. I know how to convert the lines into an integer but I don't know how to iterate through all the lines and just get the integers and store them or iterate through the lines and just get the names and store them. please help. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor You could do it with a list comprehension names = [] numbers = [] [numbers.append(int(line.strip())) if line.strip().isdigit() else names.append(line.strip()) for line in open('test.txt','rb') if line.strip()] [None, None, None, None, None, None] names, numbers (['bob', 'sue', 'jim'], [100, 250, 300]) The list comprehension would unfold to for line in open('test.txt', 'rb'): if line.strip(): if line.strip().isdigit(): numbers.append(line.strip()) else: names.append(line.strip()) And from there you can do what you like with the lists. -- Kind Regards, Christian Witts ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor