Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4244704
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:44:36+00:00 2026-05-21T03:44:36+00:00

I wonder, how to read character string like fscanf. I need to read for

  • 0

I wonder, how to read character string like fscanf. I need to read for word, in the all .txt . I need a count for each words.

collectwords = collections.defaultdict(int)

with open('DatoSO.txt', 'r') as filetxt:

for line in filetxt:
    v=""
    for char in line:
        if str(char) != " ":
          v=v+str(char)

        elif str(char) == " ":
          collectwords[v] += 1
          v=""

this way, I cant to read the last word.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T03:44:37+00:00Added an answer on May 21, 2026 at 3:44 am

    You might also consider using collections.counter if you are using Python >=2.7

    http://docs.python.org/library/collections.html#collections.Counter

    It adds a number of methods like ‘most_common’, which might be useful in this type of application.

    From Doug Hellmann’s PyMOTW:

    import collections
    
    c = collections.Counter()
    with open('/usr/share/dict/words', 'rt') as f:
        for line in f:
            c.update(line.rstrip().lower())
    
    print 'Most common:'
    for letter, count in c.most_common(3):
        print '%s: %7d' % (letter, count)
    

    http://www.doughellmann.com/PyMOTW/collections/counter.html — although this does letter counts instead of word counts. In the c.update line, you would want to replace line.rstrip().lower with line.split() and perhaps some code to get rid of punctuation.

    Edit: To remove punctuation here is probably the fastest solution:

    import collections
    import string
    
    c = collections.Counter()
    with open('DataSO.txt', 'rt') as f:
        for line in f:
            c.update(line.translate(string.maketrans("",""), string.punctuation).split())
    

    (borrowed from the following question Best way to strip punctuation from a string in Python)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ever wonder what wikipedia's database schema looks like? I recently read this thread from
I just read this post and i wonder if there is something like this
all: I wonder how to quickly read the last line of an online file,
I wonder how to trace generated SQL like DataContext in LinqToSql. I also read
I've read about XSS vulnerability in desktop applications like Skype and ICQ. I wonder
I would like to stuff an 'A' character back into stdin using ungetc on
I wonder how I should do to read objects from a list if I
I've read this question and some similar ones and I wonder if there is
I just wonder is parallel File.Read using PLINQ/Parallel can be faster? My code is
I wonder if anyone could point me in the direction where I can read

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.