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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:03:59+00:00 2026-06-03T08:03:59+00:00

I have two text files in the following format: The first is this on

  • 0

I have two text files in the following format:

The first is this on every line:

Key1:Value1

The second is this:

Key2:Value2

Is there a way I can replace Value1 in file1 by the Value2 obtained from using it as a key in file2?

For example:

file1:

foo:hello
bar:world

file2:

hello:adam
bar:eve

I would like to get:

foo:adam
bar:eve

There isn’t necessarily a match between the two files on every line. Can this be done neatly in awk or something, or should I do it naively in Python?

  • 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-03T08:04:01+00:00Added an answer on June 3, 2026 at 8:04 am

    Create two dictionaries, one for each file. For example:

    file1 = {}
    for line in open('file1', 'r'):
        k, v = line.strip().split(':')
        file1[k] = v
    

    Or if you prefer a one-liner:

    file1 = dict(l.strip().split(':') for l in open('file1', 'r'))
    

    Then you could do something like:

    result = {}
    for key, value in file1.iteritems():
        if value in file2:
            result[key] = file2[value]
    

    Another way is you could generate the key-value pairs in reverse for file1 and use sets. For example, if your file1 contains foo:bar, your file1 dict is {bar: foo}.

    for key in set(file1) & set(file2):
        result[file1[key]] = file2[key]
    

    Basically, you can quickly find common elements using set intersection, so those elements are guaranteed to be in file2 and you don’t waste time checking for their existence.

    Edit: As pointed out by @pepr You can use collections.OrderedDict for the first method if order is important to you.

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

Sidebar

Related Questions

I have the two following text files: First one: chr10 1000 1001 DEL 2.4807
I have a file which has contents on every line following this format (A,
I have a text file with the following format. The first line is the
Suppose I have two text files. The first one, called reference.txt has the following
Hey everybody, this is what I have going on. I have two text files.
I have 2 text files to compare on their first column. The following comm
I have two text files that contain columnar data of the variety position -
I have two text files, I want to place a text in the middle
I have two 3GB text files, each file has around 80 million lines. And
I have two large (~100 GB) text files that must be iterated through simultaneously.

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.