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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:44:44+00:00 2026-05-29T09:44:44+00:00

I have two tab delimited csv files (with headers) that I need to merge

  • 0

I have two tab delimited csv files (with headers) that I need to merge in python.

Also, in the merged file I want to add a column in the end to identify the files because though they have same format, they have different data that I need to separate later on.
So, I want to add a column called ‘source’ on each line of output which is 0 for file1 and 1 for file2.

I have gone far as using the csv module but the writerow adds an additioal newline character between each line it writes and this code doesn’t write anything from file2. What am I doing wrong here? Also, how do I add the extra column ‘source’ in the line object?

import os, csv

path1 = os.path.abspath("../data/file1.txt")
path2 = os.path.abspath("../data/file2.txt")
merged_path = os.path.abspath('../data/output.txt')

# merge the two files for further processing
merged_file = csv.writer(open(merged_path, 'a'), delimiter = '\t')

#file1
fg = csv.reader(open(path1, 'r'), delimiter = '\t')

for line in fg:
    if line[7] != '\N':
        merged_file.writerow(line) 

#file2
bg = csv.reader(open(path2, 'r'), delimiter = '\t')

for line in bg:
    if line[16] != '\N':
        merged_file.writerow(line) 
  • 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-29T09:44:44+00:00Added an answer on May 29, 2026 at 9:44 am

    I prefer to use the dictWriter for this. Also, your code doesn’t work because the csv library requires opening files in binary mode.

    import os, csv
    
    path1 = os.path.abspath("../data/file1.txt")
    path2 = os.path.abspath("../data/file2.txt")
    merged_path = os.path.abspath('../data/output.txt')
    
    #file1
    fg = csv.DictReader(open(path1, 'rb'), delimiter = '\t')
    
    fieldnames = fg.fieldnames
    fieldnames.append('source')
    # merge the two files for further processing
    merged_file = csv.DictWriter(open(merged_path, 'ab'), delimiter = '\t', fieldnames=fieldnames)
    merged_file.writeheader()
    
    for row in fg:
        row['source'] = os.path.basename(path1)
        merged_file.writerow(row)
    
    #file2
    bg = csv.DictReader(open(path2, 'rb'), delimiter = '\t')
    
    for row in bg:
        row['source'] = os.path.basename(path1)
        merged_file.writerow(row)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two files one is a tab delimited and one is a csv
I have two tab delimited files: File 1: 12 rows and 1 column File
I have two tab delimited files, file 1 contains identifiers and file 2 has
I have two data files in tab separated CSV format. The files are in
I have two tab-delimited genome sequence files (SAM format), and I would like to
I have text files that are Tab delimited. I created a Schema.ini like so:
Let's say I have a tab-delimited text file that contains data arranged in columns
I have two tab separated files (please see the examples below): File 1 Java
I have a data set that has two tab delimited columns that I plot
I have two tab delimited files, of 3 columns and n number of rows.

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.