Python Forum
Counting words in the last line of a file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Counting words in the last line of a file
#1
How do I count how many words there are in the last line of a file?

Here are the contents of the text file:

Quote:Welcome to your First Exam Recruit.
Only the best recruits can become agents.
Do you have what it takes?
We will test your knowledge with this field readiness exam.
It should be pretty simple, since you only know the basics so far.
Let's get started.
Best of luck recruit.

Here is the script I wrote including annotations explaining my understanding what what is going on, line by line:

with open('field.txt') as field_variable: # Opening the file as a variable
    field_variable = field_variable.readlines() # Reading the file
    list(field_variable) # Turning file contents into a list
    for i in field_variable: # Splitting each line in the list
        i.split()
    last = field_variable[-1] # Assigning the last line from the bottom by reverse slicing to a variable called, last
    for n in last: # Loop through last line
        num_chars = last.split(n) # Split last variable, assign to new variable, num_chars
        result = last.count(num_chars) # Now count number of list items
        print(result)     # Print final_chars
According to my interpreter, the problem is at line 9:

python---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-27-082decdb195e> in <module>
      7     for n in last: # Loop through last line:
      8         num_chars = last.split(n) # Split last variable, assign to new variable, num_chars
----> 9         final_chars = last.count(num_chars) # Now count number of list items
     10         print(final_chars)     # Print final_chars

TypeError: must be str, not list
Apparently Python is expecting “a string, not a list”.

There are three variables in operation at line 9: result, last and num_chars.

result is being assigned as the final integer (and end result or destination of this script - - an integer)
last contains the string of the last line of the text file, distinguished at line 6
num_chars is the integer count of the number of characters in the last line

Why is the number of characters from the last line not printing?

For my personal future reference, I’m working on Jose Portilla’s public GitHub repo for his open course on Udemy at this specific module: Python-Narrative-Journey/02-Field-Readiness-Exam-1/01-Field-Readiness-Exam-One.ipynb
Reply


Messages In This Thread
Counting words in the last line of a file - by Drone4four - May-01-2019, 09:29 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 12,537 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  Getting last line of each line occurrence in a file tester_V 1 856 Jan-31-2023, 09:29 PM
Last Post: deanhystad
  Need to match two words in a line tester_V 2 867 Nov-18-2022, 03:13 AM
Last Post: tester_V
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,362 Sep-27-2022, 01:38 PM
Last Post: buran
  Print to a New Line when Appending File DaveG 0 1,216 Mar-30-2022, 04:14 AM
Last Post: DaveG
  Find and delete above a certain line in text file cubangt 12 3,447 Mar-18-2022, 07:49 PM
Last Post: snippsat
  CSV to Text File and write a line in newline atomxkai 4 2,675 Feb-15-2022, 08:06 PM
Last Post: atomxkai
  writelines only writes one line to file gr3yali3n 2 2,369 Dec-05-2021, 10:02 PM
Last Post: gr3yali3n
  Extract a string between 2 words from a text file OscarBoots 2 1,865 Nov-02-2021, 08:50 AM
Last Post: ibreeden
  Generate a string of words for multiple lists of words in txt files in order. AnicraftPlayz 2 2,790 Aug-11-2021, 03:45 PM
Last Post: jamesaarr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020