mercredi 2 novembre 2016

Python Function that prints the number of words in a txt file and prints the first 10 words

My code:

  # -*- coding: utf-8 -*-
  def get_dictionary_wordlist():
       readFile=open('dictionary.txt', 'r')
       text=readFile.read()
       print(text)
       readFile.close
  def test_get_dictionary_wordlist():
      get_dictionary_wordlist()
      N=10
      f=open("dictionary.txt, 'r'")
      for i in range(N):
        line=f.next().strip()
        print (line)
      f.close()

  get_dictionary_wordlist()

  test_get_dictionary_wordlist()

So far I've managed to print the whole text file in list format. However, I am now trying to get it to print the number of words along with the first 10 words in the file.

Thank you

Aucun commentaire:

Enregistrer un commentaire