I have a txt file of hundreds of thousands of words. I need to get into some format (I think dictionary is the right thing?) where I can put into my script something along the lines of;
for i in word_list:
word_length = len(i)
print("Length of " + i + word_length, file=open("LengthOutput.txt", "a"))
Currently, the txt file of words is separated by each word being on a new line, if that helps. I’ve tried importing it to my python script with
From x import y
…. and similar, but it seems like it needs to be in some format to actually get imported? I’ve been looking around stackoverflow for a wile now and nothing seems to really cover this specifically but apologies if this is super-beginner stuff that I’m just really not understanding.
>Solution :
A list would be the correct way to store the words. A dictionary requires a key-value pair and you don’t need it in this case.
with open('filename.txt', 'r') as file:
x = [word.strip('\n') for word in file.readlines()]