logo CodeStepByStep logo

collapse_spaces

Language/Type: Python file input

Write a function named collapse_spaces that accepts a file name as a parameter. Your function should read that file and output it with all its tokens separated by single spaces, collapsing any sequences of multiple spaces into single spaces. For example, if the file spaced.txt contains the following text:

four      score   and

seven               years ago         our
            
fathers brought             forth
    on this          continent
a         new
            
nation

The call of collapse_spaces("spaced.txt") should produce the following output:

four score and
            
seven years ago our
            
fathers brought forth
on this continent
a new
            
nation

Each word is to appear on the same line in output as it appears in the file. Notice that lines can be blank.

Function: Write a Python function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.