-1

In Python, I'm getting a list with emptiness ( i.e. [ ] ) in it and testing mechanics indicate that its not sorting the content properly. It fails to pass the sorting of items contained in a file.

Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order.

textFile = input("Input a file name: ")

with open(textFile) as objects:

textReader = objects.readlines()
textReader.sort()
print(textReader)

Output: [ ]

Julian
  • 1
  • 4

1 Answers1

0

You didn't specify the language this needs to be in. If you would use Ruby it's like this.

File.read('input.txt').split(/\W/).uniq.sort

The only thing I believe that needs an explanation is the split(/\W/) thing. It's an regular expression (google is your best friend) which means split the text by any non textual characters like spaces and put them in an array.

peter
  • 41,770
  • 5
  • 64
  • 108