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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:42:09+00:00 2026-05-25T20:42:09+00:00

I want to merge about 8 *.csv files into one. An example file: ID,

  • 0

I want to merge about 8 *.csv files into one.

An example file:

ID, Average
34, 4.5
35, 5.6
36, 3.4

Another file could be:

ID, Max
34, 6
35, 7
36, 4

And I need the output to be:

ID, Average, Max
34, 4.5, 6
35, 5.6, 7
36, 3.4, 4

This only half works…. it appends all the data into the same two columns.

import glob, string

outfile = open('<directory>/<fileName>.csv','a')    
files = glob.glob(r"<directory>/*.csv")

for y in files:
    newfile = open(y,'r+')       
    data = newfile.read()
    newfile.close()
    outfile.writerow(y)

How can I append the data to new columns, and not repeat the “ID” field?

  • 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-25T20:42:09+00:00Added an answer on May 25, 2026 at 8:42 pm

    You have three problems here.

    1. Read in each of the csv files
    2. Merge on a common field
    3. Write the merged data to a new csv file

    Code

    #!/usr/bin/env python
    import argparse, csv
    if __name__ == '__main__':
    
        parser = argparse.ArgumentParser(description='merge csv files on field', version='%(prog)s 1.0')
        parser.add_argument('infile', nargs='+', type=str, help='list of input files')
        parser.add_argument('--out', type=str, default='temp.csv', help='name of output file')
        args = parser.parse_args()
        data = {}
        fields = []
    
        for fname in args.infile:
            with open(fname, 'rb') as df:
                reader = csv.DictReader(df)
                for line in reader:
                    # assuming the field is called ID
                    if line['ID'] not in data:
                        data[line['ID']] = line
                    else:
                        for k,v in line.iteritems():
                            if k not in data[line['ID']]:
                                data[line['ID']][k] = v
                    for k in line.iterkeys():
                        if k not in fields:
                            fields.append(k)
                del reader
    
        writer = csv.DictWriter(open(args.out, "wb"), fields, dialect='excel')
        # write the header at the top of the file
        writer.writeheader()
        writer.writerows(data)
        del writer
    

    Note that this will ignore data with an identical field name.

    An alternative to the parser section is this:

    #!/usr/bin/env python
    import glob, csv
    if __name__ == '__main__':
    
        infiles = glob.glob('./*.csv')
        out = 'temp.csv'
        data = {}
        fields = []
    
        for fname in infiles:
            df = open(fname, 'rb')
            reader = csv.DictReader(df)
            for line in reader:
                # assuming the field is called ID
                if line['ID'] not in data:
                    data[line['ID']] = line
                else:
                    for k,v in line.iteritems():
                        if k not in data[line['ID']]:
                            data[line['ID']][k] = v
                for k in line.iterkeys():
                    if k not in fields:
                        fields.append(k)
            del reader
            df.close()
    
        writer = csv.DictWriter(open(out, "wb"), fields, dialect='excel')
        # write the header at the top of the file
        writer.writeheader()
        writer.writerows(data)
        del writer
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to merge 2 bzip2'ed files. I tried appending one to another: cat
I want to merge multiple rss feeds into a single feed, removing any duplicates.
I want to merge one .NET DLL assembly and one C# Class Library project
I have a list of List<KeyvaluePair<DateTime, int>> , which I want to merge into
I want to bulk insert about 700 records into the Android database on my
I have question about integrate Open office with PHP. I want make mail merge
I have 8 bool variables, and I want to merge them into a byte.
I have a number of CSV files with hundreds of columns and about 50,000
I have 500 csv files , each of them's size is about 10~20M. for
I want to merge the results of two tables in to one. Please refer

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.