2 Replies - 545 Views - Last Post: 02 February 2012 - 06:59 PM Rate Topic: -----

Topic Sponsor:

#1 cthomas1489  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 81
  • Joined: 20-June 11

Counting words in a txt file.

Posted 01 February 2012 - 04:10 PM

So I'm attempting to help my girlfriend with her python assignment and I'm not very familiar with Python coding/syntax.

Basically she has a text file that has a list of words and needs to count how many words are in the file. It would actually appear to me that she only needs to count the number of lines since there are no lines with multiple words (ie. each line is one word, in a list format). So we don't need to worry about getting rid of whitespace or anything. We want to output a text file with the number of words written in it as well.

So:

filename = 'C:\Users\Chris\Desktop\test.txt'

int words = 0
textfile = open(filename, 'r')

for wordcount in textfile
  
  Something here  

  words++



I'm not familiar with what to use in python. In java I would use a while loop as long as input.hasNextLine() and increment the counter every time I input.nextLine();

What would the equivalent be here?

Is This A Good Question/Topic? 0
  • +

Replies To: Counting words in a txt file.

#2 Simown  Icon User is online

  • D.I.C Regular
  • member icon

Reputation: 233
  • View blog
  • Posts: 455
  • Joined: 20-May 10

Re: Counting words in a txt file.

Posted 01 February 2012 - 06:24 PM

First of all, you need to escape the backslashes in a path name, if you don't understand what that means, this topic came up recently which explains it in detail: http://www.dreaminco...hon-file-error/

You were close, but you can't write variable++ in Python, it's just not valid syntax.

Python binds types to variables at runtime, so called "dynamic typing" - int variable is not valid syntax, the interpreter will work out the type and stick to it.

It's not only Python that does this, a "for item in" increments to the next value each loop, so you don't need to explicitly ask for it.

Another thing is that you are counting each character in the for loop, assuming the words are separated by spaces, the best thing to do would be to split the text file on each empty character.

filename = r'C:\Users\Chris\Desktop\test.txt'

words = 0
textfile = open(filename, 'r')

# For each word in the file, split the text on the empty character
for wordcount in textfile.readlines().split(" "):
  #Count the word
  words += 1

#Print the result
print("The number of words are: " + words)



This post has been edited by Simown: 01 February 2012 - 06:31 PM

Was This Post Helpful? 0
  • +
  • -

#3 Vblaster  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 16-January 11

Re: Counting words in a txt file.

Posted 02 February 2012 - 06:59 PM

Shorter, and no for loop:
filename = r'C:\Users\Chris\Desktop\test.txt'

textfile = open(filename, 'r')
#Print the result
print("The number of words are: " + len(textfile.split(" ")))



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1