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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:24:37+00:00 2026-06-17T10:24:37+00:00

I am trying to compare values of a particular column between 2 csv. I

  • 0

I am trying to compare values of a particular column between 2 csv. I tried the following code for the same. However, I am not getting any output and no error too. Please help me with this

with open("File1.csv", "rb") as in_file1, open("File2.csv", "rb") as in_file2,open("File3.csv", "wb") as out_file:
   reader1 = csv.reader(in_file1)
   reader2 = csv.reader(in_file2)
   writer = csv.writer(out_file)
   for row2 in reader2:
       for row1 in reader1:
           if row2[0] == row1[0]:
               row2[1] = row1[1]
       writer.writerow(row2)

Here is how the data looks like:

File 1

A 100
B 200
C 300
D 400
E 500

FIle 2

A
C
E
E
E
D

File 3 (Should be)

A 100
C 300
E 500
E 500
E 500
D 400
  • 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-17T10:24:38+00:00Added an answer on June 17, 2026 at 10:24 am

    File1.csv is a mapping. Read it first and store it in a dictionary. Then iterate over File2.csv and write it to File3.csv together with the value retrieved from the mapping dictionary.

    The following code works for your example:

    with open("File1.csv", "rb") as in_file1:
        d = dict(csv.reader(in_file1, delimiter=' '))
    
    with open("File2.csv", "rb") as in_file2, open("File3.csv", "wb") as out_file:
        writer = csv.writer(out_file, delimiter=' ')
        for rec in csv.reader(in_file2, delimiter=' '):
            writer.writerow((rec[0], d[rec[0]]))
    

    Just for an illustration, d looks like this:

    {'A': '100', 'B': '200', 'C': '300', 'D': '400', 'E': '500'}
    

    The values are strings (not integers), but this is not a problem, since we are just printing them into a file.

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

Sidebar

Related Questions

I am trying to compare the following values: gType = [[UILabel alloc]init]; if (gType
I have the following problem. I am trying to compare two datetime values in
I'm trying to compare two values in the same table, and check if there
I am trying to compare values on data frame rows, and removing all the
I'm trying to compare two arrays and get only the values that exist on
When trying to compare software versions 5.12 to 5.8, version 5.12 is newer, however
I'm trying to compare a couple of array values stored in a flat file
I´m trying to compare values from an array with values in a combobox in
I am trying to compare two int values with a method. I have put
I am trying to compare performance between raw pointers, boost shared_ptr and boost weak_ptr.

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.