Python Forum
To count a number of strings in a txt file with python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
To count a number of strings in a txt file with python
#1
I have a .txt file that contain ten words(for example ten names) and i wanted to know how can i count the number of names, in this case 10. If i use len(names), the console gives me the length of the word, not the number of the names. Thank you
Reply
#2
Hi,

Here is a link that shows a number of examples to tackle what looks like the same problem. The thread is calledSign up

How to get line count cheaply in Python?


http://stackoverflow.com/questions/84505...-in-python

You may want to watch out that these refer to the same flavour of Python (i.e. Pre/Post version Three)

Good Luck

Bass
Reply
#3
Post what you have tried with code jpeich,read this.

@Bass
We try to answer relative simple question like this yourself in the forum.
It can be okay to reefer an answer in other forum in more difficult cases.
The scale is different to "hundreds of thousands of lines" in that thread.

We can all just search Stack Overflow and post link to answer there,
but that's not much effort and not what we want this forum to be about.
Reply
#4
You don't need python to count words. 

$ echo "We try to answer relative simple question like this yourself in the forum" | wc -w

13
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
(Apr-22-2017, 09:07 PM)snippsat Wrote: @snippsat

Quote:@Bass
We try to answer relative simple question like this yourself in the forum.
It can be okay to reefer an answer in other forum in more difficult cases.
The scale is different to "hundreds of thousands of lines" in that thread.

We can all just search Stack Overflow and post link to answer there,
but that's not much effort and not what we want this forum to be about.

Understood. Thank you for the advice and explanation.
Bass
Reply
#6
>>> text = """
... spam eggs
... fish
... carrot dog worm
... mouse keyboard history lesson""".split()
>>>
>>> # with open(filename) as text:
...
>>> word_count = 0
>>> for line in text:
...   word_count += len(line.split())
...
>>> word_count
10
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] Pad strings to always get three-digit number? Winfried 2 349 Jan-27-2024, 05:23 PM
Last Post: Winfried
  Trying to understand strings and lists of strings Konstantin23 2 764 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,546 May-01-2023, 09:06 PM
Last Post: deanhystad
  Row Count and coloumn count Yegor123 4 1,331 Oct-18-2022, 03:52 AM
Last Post: Yegor123
  Unable to count the number of tries in guessing game. Frankduc 7 1,914 Mar-20-2022, 08:16 PM
Last Post: menator01
  Splitting strings in list of strings jesse68 3 1,773 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
Question Help to find the largest int number in a file directory SalzmannNicholas 1 1,638 Jan-13-2022, 05:22 PM
Last Post: ndc85430
  Problem : Count the number of Duplicates NeedHelpPython 3 4,386 Dec-16-2021, 06:53 AM
Last Post: Gribouillis
  all i want to do is count the lines in each file Skaperen 13 4,843 May-23-2021, 11:24 PM
Last Post: Skaperen
  Reducing JSON character count in Python for a Twitter Bot johnmitchell85 2 53,119 Apr-28-2021, 06:08 PM
Last Post: johnmitchell85

Forum Jump:

User Panel Messages

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