How do I strip a new line?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anderson1373
    New Member
    • Nov 2009
    • 23

    #1

    How do I strip a new line?

    It only catches the comparison to average for the first name and i need to strip the new line.
    Can you help?


    Code:
    scores = []
    names = []
    infile = open("bowlingscores.txt", "r")
    for i, line in enumerate(infile):
    	if i % 2 == 1:
    		scores.append(int(line))
    	else:
    		names.append(line)
    average = float(sum(scores) / len(scores))
    print (names)
    print (scores)
    outfile = open("bowlingaverages.txt", "w")
    for s in zip(names, scores):
    	if scores > average:
    		scores = "above average"
    	elif scores < average:
    		scores = "below average"
    	else:
    		scores = "perfect"
    outfile.write("%s" % names)
    outfile.write("%s" % scores)
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    String method strip() strips leading and trailing whitespace such as newline characters.
    Code:
    >>> 
    >>> line = "123456\n"
    >>> line.strip()
    '123456'
    >>>

    Comment

    • anderson1373
      New Member
      • Nov 2009
      • 23

      #3
      Where in my code would it go?
      Also how do I make it go through my loop every time? It is only going through once.

      Comment

      • anderson1373
        New Member
        • Nov 2009
        • 23

        #4
        Here is how i changed it:
        Code:
        scores = []
        names = []
        averagescore = []
        infile = open("bowlingscores.txt", "r")
        for i, line in enumerate(infile):
        	if i % 2 == 1:
        		scores.append(int(line))
        	else:
        		names.append(line)
        average = float(sum(scores) / len(scores))
        print(average)
        print (names)
        print (scores)
        outfile = open("averages.txt", "w")
        scores = str(scores)
        for score in (scores):
        	if score > average:
        		averagescore.append("above average")
        	elif score < average:
        		averagescore.append("below average")
        	else:
        		averagescore.append("perfect")
        for s in zip(names, averagescore):
        	outfile.write(s)
        It keeps giving me this error on the line that reads outfile.write(s ):
        TypeError: argument 1 must be a string or read only character buffer, not tuple

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          It should be:
          Code:
          for tup in zip(names, averagescore):
              outfile.write(" ".join(tup))

          Comment

          Working...