Remove repeated words using python

2

I have a text file with many words repeated. I need every word in the file to appear only once.

 import  codecs

 wordList = codecs.open('Arquivo.txt' , 'r')
 wordList2 = codecs.open('Arquivo2.txt', 'w')

 for x in range(len(wordList)) :
    for y in range(x + 1, len(wordList ) ):
        if wordList[x] == wordList[y]:
            wordList2.append(wordList[x] )
        for y in wordList2:
             wordList.remove(y)

Error submitted

     for x in range(len(wordList)):
 TypeError: object of type 'file' has no len()
    
asked by anonymous 06.09.2017 / 01:42

1 answer

2

Instead of opening the files like this:

wordList = codecs.open('Arquivo.txt' , 'r')
wordList2 = codecs.open('Arquivo2.txt', 'w')

Try this:

wordList = codecs.open('Arquivo.txt' , 'r').readlines()
wordList2 = codecs.open('Arquivo2.txt', 'w')

I recommend that you read the python encoding style guide . The use of CamelCase for variable names is not recommended in python.

    
06.09.2017 / 03:01