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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:28:15+00:00 2026-06-15T20:28:15+00:00

I have two files File1: IdName1 Info1 Info2 Info3 #Info: from program1 for name1

  • 0

I have two files

File1:

IdName1 Info1 Info2 Info3   #Info: from program1 for name1  #Info: from program2 for name1
IdName2 Info1 Info2 Info3   #Info: from program1 for name2  #Info: from program2 for name2
IdName4 Info1 Info2 Info3   #Info: from program1 for name4  
IdName3 Info1 Info2 Info3   #Info: from program1 for name3  #Info: from program2 for name3

File2:

# ProgramInfo
# Query: IdName1 Info1 Info2 Info3
# DatabaseInfo
# FiledInfo
line1
line2
# ProgramInfo
# Query: IdName2 Info1 Info2 Info3
# DatabaseInfo
# FiledInfo
# ProgramInfo
# Query: IdName4 Info1 Info2 Info3
# DatabaseInfo
# FiledInfo
line1
line2
line3
line4

Now I need to check if “IdName1 Info1 Info2 Info3” is in File2 after “#Query: ” and if it is I need to split up the iformation from that line in File1 and
insert it in File2 before the corresponding “# ProgramInfo” line. The out put file should look like this:

OutputFile:

# IdName1 Info1 Info2 Info3
# Info: from program1 for name1 
# Info: from program2 for name1
# ProgramInfo
# Query: IdName1 Info1 Info2 Info3
# DatabaseInfo
# FiledInfo
line1
line2
# IdName2 Info1 Info2 Info3 
# Info: from program1 for name2 
# Info: from program2 for name2
# ProgramInfo
# Query: IdName2 Info1 Info2 Info3
# DatabaseInfo
# FiledInfo
# IdName4 Info1 Info2 Info3 
# Info: from program1 for name4 
# ProgramInfo
# Query: IdName4 Info1 Info2 Info3
# DatabaseInfo
# FiledInfo
line1
line2
line3
line4

My question is now, how do I add the corresponding three lines into File2, I have been trying something like this:

import sys
def programs_info_comb(fileName1, fileName2):
    my_file1 = open(fileName1, "r")
    my_line1=my_file1.readlines()
    my_file2 = open(fileName2, "r")
    my_line2=my_file2.readlines() 
    for line1 in my_line1: 
        (name1, info1, info2)= line1.strip().split("\t")
        for line2 in my_line2:
            if line2.startswith("# Q"):
                name2 = line2[9:-1]
                if name1 == name2:
                    #### here Im lost how to tell where I want those next two lines to be printed
                    print "#"+" "+name1
                    print info1
                    print info2
    my_file1.close
    my_file2.close
if __name__== "__main__":
    programs_info_comb(sys.argv[1], sys.argv[2])

There is probably a better easier way to this, all help will be gratefully accepted
Thank you for your time
Daeja

  • 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-15T20:28:16+00:00Added an answer on June 15, 2026 at 8:28 pm
    import sys
    def programs_info_comb(fileName1, fileName2):
        my_file1 = open(fileName1, "r")
        my_line1=my_file1.readlines()
        my_file1.close()
    
        my_file2 = open(fileName2, "r")
        my_line2=my_file2.readlines() 
        my_file2.close()
    
        # load file1 into a dict for lookup later
        infoFor = dict()
        for line1 in my_line1: 
            parts = line1.strip().split("\t")
            infoFor[parts[0]] = parts[1:] 
    
        # iterate over line numbers to be able to refer previous line numbers
        for line2 in range(len(my_line2)):
            if my_line2[line2].startswith("# Q"):
                name2 = my_line2[line2][9:-1]
                # lookup
                if infoFor.has_key(name2):
                    print '# ' + name2
            for info in infoFor[name2]:
                        print info
                # print programinfo and query lines
                    print my_line2[line2-1],
                    print my_line2[line2],
        # skip program info always
            elif my_line2[line2].startswith("# ProgramInfo"):
                pass
        # otherwise just print as is
            else:
                print my_line2[line2],
    
    if __name__== "__main__":
        programs_info_comb(sys.argv[1], sys.argv[2])
    

    I have loaded file1 into a dictionary for lookup later and sent the output to stdout. Before sending the output I’ve checked the type of line I’m on and output accordingly.

    Here’s the o/p :-

    C:\>python st.py f1.txt f2.txt
    # IdName1 Info1 Info2 Info3
    #Info: from program1 for name1
    #Info: from program2 for name1
    # ProgramInfo
    # Query: IdName1 Info1 Info2 Info3
    # DatabaseInfo
    # FiledInfo
    line1
    line2
    # IdName2 Info1 Info2 Info3
    #Info: from program1 for name2
    #Info: from program2 for name2
    # ProgramInfo
    # Query: IdName2 Info1 Info2 Info3
    # DatabaseInfo
    # FiledInfo
    # IdName4 Info1 Info2 Info3
    #Info: from program1 for name4
    # ProgramInfo
    # Query: IdName4 Info1 Info2 Info3
    # DatabaseInfo
    # FiledInfo
    line1
    line2
    line3
    line4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have two different files somewhat like this: file1.py from file2 import *
I have two files, and I need to compare the second field from File1
I have two FASTA files: file1.fasta >foo ATCGGGG >bar CCCCCC file2.fasta >qux ATCGGAAA What
I have two files. file1 has the data like belowing containing only one column.
I have the following two files: file1.c int main(){ foo(); return 0; } file2.c
I have two files, file1 and file2. I have to modify file1 in a
I have two C++ files, say file1.cpp and file2.cpp as //file1.cpp #include<cstdio> void fun(int
I have two files I merged them based key using below code file1 -------------------------------
here is the question: I have two files: file1: aaa bbb ccc ddd file2:
i have two java files file1 and file2 as follows in package pak file1:

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.