Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python3.2
- # -*- coding: utf-8 -*-
- # main.py
- """ Task: Exercise 8.4
- Download a copy of the file from www.py4inf.com/code/romeo.txt
- Write a program to open the file romeo.txt and read it line by line. For
- each line, split the line into a list of words using the split function.
- For each word, check to see if the word is already in a list. If the word
- is not in the list, add it to the list.
- When the program completes, sort and print the resulting words in
- alphabetical order.
- Enter file: romeo.txt
- ['Arise', 'But', 'It', 'Juliet', 'Who', 'already',
- 'and', 'breaks', 'east', 'envious', 'fair', 'grief',
- 'is', 'kill', 'light', 'moon', 'pale', 'sick', 'soft',
- 'sun', 'the', 'through', 'what', 'window',
- 'with', 'yonder']
- """
- ''' Main '''
- try:
- fhand = open('romeo.txt')
- except:
- print("File not found!")
- exit()
- content = list()
- for line in fhand:
- for item in line.split():
- if item not in content[:]:
- content += [item]
- contentSorted = content[:] # Copy of content
- contentSorted.sort()
- print(contentSorted)
- try:
- fhand.close()
- except:
- exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement