Separate words into alphabetical files in python

137 Views Asked by At

I am working on a project in Python and trying to separate a list of words out into alphabetical files. So any word starting with 'a' or 'A' would go into an 'A.html' file. I am able to create the file and have all the words that start with the letter, but I need to do it recursively so that it will go through all the letters and put them into different files. Here is some of the code: class LetterIndexPage(object):

   def __init__(self, wordPage):
       self.alphaList = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','Numbers','Special Characters']

   def createLetterPages(self):
       if not os.path.exists('A.html'):
           file('A.html', 'w')
       letterFileName = 'A.html'
       letterItemList = []
       for item in wordItems():
           if item[:1] == 'a' or item[:1] == 'A':
               letterItemList.append(item)
       letterItems = reduce(lambda letterItem1, letterItem2: letterItem1 + letterItem2, letterItemList)
       return letterItems

The wordItems() method returns all the text from a web page. I am not sure where to go from here. Can anyone help?

2

There are 2 best solutions below

0
On
from itertools import groupby
import requests
page = requests.get('http://www.somepage.com/some.txt')
all_words = page.text.split()
groups = groupby(sorted(all_words),lambda x:x[0].lower())
for g in groups:
   with open("%s.html"%g[0],"a") as f:
        f.write("\n".join(g[1]))

should work I think (without testing it...)

0
On

Open the files first, do the work, then close them:

from string import ascii_uppercase

output_files = {letter: open(letter + '.html', 'w') for letter in ascii_uppercase}
for word in list_of_words:
    output_files[word[0].upper()].write(word + '\n')

for of in output_files:
    of.close()