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

  • SEARCH
  • Home
  • 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 7907349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:23:16+00:00 2026-06-03T11:23:16+00:00

i have multiple files each containing 8/9 columns. for a single file : I

  • 0

i have multiple files each containing 8/9 columns.

for a single file : I have to read last column containing some value and count the number of occurrence of each value and then generate an outfile.

I have done it like:

inp = open(filename,'r').read().strip().split('\n')  
out = open(filename,'w')  
from collections import Counter  
C = Counter()  
for line in inp:  
    k = line.split()[-1] #as to read last column  
    C[k] += 1  
for value,count in C.items():  
    x = "%s   %d" % (value,count)  
    out.write(x)  
    out.write('\n')  
out.close()  

now the problem is it works fine if I have to generate one output for one input. But I need to scan a directory using glob.iglobfunction for all files to be used as input. And then have to perform above said program on each file to gather result for each file and then of course have to write all of the analyzed results for each file into a single OUTPUT file.

NOTE: During generating single OUTPUT file if any value is found to be getting repeated then instead of writing same entry twice it is preferred to sum up the ‘count’ only. e.g. analysis of 1st file generate:

123 6  
111 5  
0   6  
45  5  

and 2nd file generate:

121 9  
111 7  
0   1  
22  2  

in this case OUTPUT file must be written such a way that it contain:

123 6  
111 12 #sum up count no. in case of similar value entry  
0   7  
45  5  
22  2  

i have written prog. for single file analysis BUT i’m stuck in mass analysis section.
please help.

  • 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-06-03T11:23:21+00:00Added an answer on June 3, 2026 at 11:23 am

    Initialise a empty dictionary at the top of the program,
    lets say, dic=dict()

    and for each Counter update the dic so that the values of similar keys are summed and the new keys are also added to the dic

    to update dic use this:

    dic=dict( (n, dic.get(n, 0)+C.get(n, 0)) for n in set(dic)|set(C) )

    where C is the current Counter, and after all files are finished write the dic to the output file.

    import glob
    from collections import Counter
    dic=dict()
    g_iter = glob.iglob(r'c:\\python32\fol\*')
    for x in g_iter:
    
        lis=[]
        with open(x) as f:
            inp = f.readlines()
        for line in inp:
            num=line.split()[-1]
            lis.append(num)
        C=Counter(lis)
        dic=dict( (n, dic.get(n, 0)+C.get(n, 0)) for n in set(dic)|set(C) )
    for x in dic:
        print(x,'\t',dic[x])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have merged multiple jpeg files into one single .bin file. ..... ......... while(true){
I have a few hundred Excel files, where each file has some data on
I have a folder with multiple sub-folders and each sub-folder contains 10-15 files. I
I have a 200kb file, what I use in multiple pages, but on each
I have multiple (8) WAR files and 1 EAR file that I want to
I have 75+ different text files containing some lines of dialogue spoken by characters
I have a PHP application containing multiple modules and various settings within each module.
I have thousands of text files containing multiple JSON objects, but unfortunately there is
I have to load an array of doubles from a file, multiply each element
If I have multiple files marked, how do I find/visit all those marked files

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.