Advertisement
Mars83

8-4

Oct 9th, 2011
663
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. #! /usr/bin/env python3.2
  2. # -*- coding: utf-8 -*-
  3.  
  4. # main.py
  5. """ Task: Exercise 8.4
  6.    Download a copy of the file from www.py4inf.com/code/romeo.txt
  7.    Write a program to open the file romeo.txt and read it line by line. For
  8.    each line, split the line into a list of words using the split function.
  9.    For each word, check to see if the word is already in a list. If the word
  10.    is not in the list, add it to the list.
  11.    When the program completes, sort and print the resulting words in
  12.    alphabetical order.
  13.    
  14.    Enter file: romeo.txt
  15.    ['Arise', 'But', 'It', 'Juliet', 'Who', 'already',
  16.    'and', 'breaks', 'east', 'envious', 'fair', 'grief',
  17.    'is', 'kill', 'light', 'moon', 'pale', 'sick', 'soft',
  18.    'sun', 'the', 'through', 'what', 'window',
  19.    'with', 'yonder']
  20. """
  21.  
  22. ''' Main '''
  23. try:
  24.     fhand = open('romeo.txt')
  25. except:
  26.     print("File not found!")
  27.     exit()
  28. content = list()
  29. for line in fhand:
  30.     for item in line.split():
  31.         if item not in content[:]:
  32.             content += [item]
  33. contentSorted = content[:]  # Copy of content
  34. contentSorted.sort()          
  35. print(contentSorted)
  36. try:
  37.     fhand.close()
  38. except:
  39.     exit()
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement