write a program that takes as an input a text file. Your program should print all the unique words in the file in alphabetical order.
write a program that takes as an input a text file. Your program should print all the unique words in the file in alphabetical order.
def unique_file(input_filename, output_filename):
input_file = open(input_filename, 'r')
file_contents = input_file.read()
input_file.close()
duplicates = []
word_list = file_contents.split()
file = open(output_filename, 'w')
for word in word_list:
if word not in duplicates:
duplicates.append(word)
file.write(str(word) + "\n")
file.close()
unique_file('sample.txt','unique_output.txt')
for line in sorted(open('unique_output.txt')):
print(line, end='')
Print this post
def unique_file(input_filename, output_filename):
input_file = open(input_filename, 'r')
file_contents = input_file.read()
input_file.close()
duplicates = []
word_list = file_contents.split()
file = open(output_filename, 'w')
for word in word_list:
if word not in duplicates:
duplicates.append(word)
file.write(str(word) + "\n")
file.close()
unique_file('sample.txt','unique_output.txt')
for line in sorted(open('unique_output.txt')):
print(line, end='')
Output
This
is
the
most
import
task.
you
have
performed
in
a
while.
python
rocks
yeah.
Output is not come
ReplyDeleteThanks for the informative Content. I learned a lot here. Keep sharing more like this.
ReplyDeleteAWS and Salesforce
Salesforce Integrations