0

I have to evaluate a text file that contains a poorly formatted essay that I must reformat. The first step is to remove all extra spaces in the sentences. I decided to read in the file and then put all the lines into a string and then I put the lines that contains sentences into its own separate list. Now I have trouble with deciding how to remove the extra spaces in the list and was wondering if theres a built in method that I can use to remove extra spaces?

Here is an example of a sentence in my list:

["Albuquerque is my     turkey and he's   feathered and    he's      fine, And    he"]

and the code I have so far :

def remove_extra_whitespaces():
    fileList= []
    removeList= []
    infile= open("essay1.txt", 'r')
    for line in infile:
        if (len(line))>0:
            fileList.append(line.strip())

        else:
            fileList.append(line)
    print (len(fileList[4]))
    for k in range(len(fileList)):
        if (len(fileList[k]))>0:
            #" ".join(fileList[k])
            removeList.append(fileList[k])
Etty Ets
  • 85
  • 2
  • 6
  • You could have found many answers for this by literally copy-pasting your question's exact title into a Google search. You are expected to do at least a little bit of research before posting a question - please do that first in the future. – TigerhawkT3 Feb 21 '17 at 02:23

1 Answers1

1

I think this is the easiest way:

import re
str = "Albuquerque is my     turkey and he's   feathered and    he's      fine, And    he"
print re.sub(r' +', ' ', str)

Output:

Albuquerque is my turkkey and he's feathered and he's fine, And he
McGrady
  • 10,869
  • 13
  • 47
  • 69