Reputation: 3
So I have tried to make it so that I can extract words in a file with every English word from random letters a generator gives me. Then I would like to add the found words to a list. But I am having a bit of a problem acquiring this result. Could you help me please?
This is what I have tried:
import string
import random
def gen():
b = []
for i in range(100):
a = random.choice(string.ascii_lowercase)
b.append(a)
with open('allEnglishWords.txt') as f:
words = f.read().splitlines()
joined = ''.join([str(elem) for elem in b])
if joined in words:
print(joined)
f.close()
print(joined)
gen()
if you are wondering where I got the txt file it is located here http://www.gwicks.net/dictionaries.htm. I downloaded the one labeled ENGLISH - 84,000 words the text file
Upvotes: 0
Views: 452
Reputation: 13242
import string
import random
b = []
for i in range(100):
a = random.choice(string.ascii_lowercase)
b.append(a)
b = ''.join(b)
with open('engmix.txt', 'r') as f:
words = [x.replace('\n', '') for x in f.readlines()]
output=[]
for word in words:
if word in b:
output.append(word)
print(output)
Output:
['a', 'ad', 'am', 'an', 'ape', 'au', 'b', 'bi', 'bim', 'c', 'cb', 'd', 'e',
'ed', 'em', 'eo', 'f', 'fa', 'fy', 'g', 'gam', 'gem', 'go', 'gov', 'h',
'i', 'j', 'k', 'kg', 'ko', 'l', 'le', 'lei', 'm', 'mg', 'ml', 'mr', 'n',
'no', 'o', 'om', 'os', 'p', 'pe', 'pea', 'pew', 'q', 'ql', 'r', 's', 'si',
't', 'ta', 'tap', 'tape', 'te', 'u', 'uht', 'uk', 'v', 'w', 'wan', 'x', 'y',
'yo', 'yom', 'z', 'zed']
Upvotes: 1
Reputation: 966
Focusing on acquiring this result, assume your words are seperated by a single space:
with open("allEnglishWords.txt") as f:
for line in f:
for word in line.split(" "):
print(word)
Also, you don't need f.close()
inside a with
block.
Upvotes: 0