Need to open text file, print random word with over 5 characters python -


import random dictionary = open('word_list.txt', 'r') line in dictionary:      in range(0, len(line)):         if >= 5:                 word = random.choice(line) dictionary.close() 
  1. this code doesnt seem work me
  2. here link file if helps http://vlm1.uta.edu/~athitsos/courses/cse1310_summer2013/assignments/assignment8/word_list.txt

import random  open('word_list.txt', 'r') f:     words = [word.rstrip() word in f if len(word) > 5]  print random.choice(words) 

as @ashwini-chaudhary correctly pointed out, word on each step of iteration has newline \n @ end - that's why need use rstrip().


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -