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 8037877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:03:28+00:00 2026-06-05T03:03:28+00:00

I have multiple files with data written in two column looks like: asp 1.88

  • 0

I have multiple files with data written in two column looks like:

asp 1.88
gln 3.10
arg 0.99
his 1.11

and

asp 0.99
gln 1.11
arg 0.08
his 0.01

and so on.

What I want to do is add them up and write to a new file like this:

asp 2.87
gln 4.21
arg 1.07
his 1.12

I was trying by importing Counter but it is not doing well. I also tried like this:

inp = ('c"/users/ansm/desktop/xx.txt','r').read().strip().split('\n')
inp2 = ('c"/users/ansm/desktop/xyz.txt','r').read().strip().split('\n')
c = Counter(inp)
d = Counter(inp2)
print c+d

but this code doesn’t add up the values.

Is there any other way to do it without using Counter?
I was using glob.iglob to iterate throughout the folder for files with .txt format and then want to process them to get above mentioned result. Will using Counter be the most efficient way to do it or would some other way even better?

  • 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-05T03:03:29+00:00Added an answer on June 5, 2026 at 3:03 am

    Create the Counters using this construct:

    c = Counter(dict((x.split()[0], float(x.split()[1])) for x in inp))
    d = Counter(dict((x.split()[0], float(x.split()[1])) for x in inp2))
    

    The result c+d will be:

    Counter({'gln': 4.21, 'asp': 2.87, 'his': 1.12, 'arg': 1.07})
    

    EDIT for many files:

    s = Counter()
    for filename in glob.iglob('*.txt'):
        with open(filename, 'r') as f:
            for line in f:
                k, v = line.split()
                s[k] += float(v)
    print s
    with open('sum.txt', 'w') as f:
        for k, v in sorted(s.iteritems()):
            f.write('{0} {1}\n'.format(k, v))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multiple text files with logged data like this: 6/23/09 17:00 0.443 6/23/09
I have multiple XML files in a folder,so I written script like this to
I would like to have multiple files in a folder with are managed by
We have multiple log files like database log, weblog, quartzlog in our application. Any
I have multiple (8) WAR files and 1 EAR file that I want to
I have written a code for joining two wave files.It works fine when i
I have some data files that are written as tag = value, where tag
I have a jquery app with multiple data-roles=page in one html file. Is it
i have multiple files each containing 8/9 columns. for a single file : I
I have multiple files ( > 1000) with same name in different subdirectories dir1/out.txt

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.