#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2

    Question About Removing Newline Characters in Function


    I'm having issues with trying to make a program that will read words from a given file, line by line, but also to remove all newlines that exist.

    It works, just, there's clearly still a new line when it prints out what's inside the text file.

    For example, I made a non-sensical text file named 'Gold.txt' that contains the following text:

    Gold
    Sun
    Is

    My Favorite
    Time of Day


    The program will print this out, but will STILL include that newline character in the middle between 'Is' and 'My Favorite'

    Anyway, here's the code for my function, thanks a bunch! :

    Code:
    def read_words(words_file):
        """ (file open for reading) -> list of str
    
        Return a list of all words (with newlines removed) from open file
        words_file.
    
        Precondition: Each line of the file contains a word in uppercase characters
        from the standard English alphabet.
        """
    
    
    
    
        words_file = open(words_file, 'r')
    
        # This will oepn up the file for reading, hence the use of 'r'
    
        line = words_file.readline()
    
        # This will assign each line to the variable line
    
        while line != '':
    
            # Here's a while loop that will make sure that the line in question
            # is NOT a blank line aka a newline character that results from enter
    
    
            print(line, end = '')
    
            # This will then print the line itself and end with an empty character
    
            line = words_file.readline()
    
            # If we don't redefine line within the loop, the loop itself will become
            # endless, and continue to print the same line OVER AND OVER AGAIN
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    509
    Rep Power
    33
    No it doesn't (hint use strip()
    # Here's a while loop that will make sure that the line in question
    # is NOT a blank line aka a newline character that results from enter
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Thanks a bunch for the tip to add rstrip! I believe that function was mentioned in the course, but a while back, so I forgot that I could totally use that to strip any given character a of a given line (I think it was introduced earlier on with strings)

    Anyway, thanks a bunch! It totally works now!

    The line I added is in bold:
    Code:
    def read_words(words_file):
        """ (file open for reading) -> list of str
    
        Return a list of all words (with newlines removed) from open file
        words_file.
    
        Precondition: Each line of the file contains a word in uppercase characters
        from the standard English alphabet.
        """
    
    
    
    
        words_file = open(words_file, 'r')
    
        # This will oepn up the file for reading, hence the use of 'r'
    
        line = words_file.readline()
    
        # This will assign each line to the variable line
    
        while line != '':
    
            # Here's a while loop that will make sure that the line in question
            # is NOT a blank line aka a newline character that results from enter
    
            line = line.rstrip('\n')
    
    
            print(line, end = '')
    
    
            # This will then print the line itself and end with an empty character
    
            line = words_file.readline()
    
            # If we don't redefine line within the loop, the loop itself will become
            # endless, and continue to print the same line OVER AND OVER AGAIN

IMN logo majestic logo threadwatch logo seochat tools logo