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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:03:49+00:00 2026-06-01T03:03:49+00:00

I need to combine lines in two files, basing in the condition, that in

  • 0

I need to combine lines in two files, basing in the condition, that in the line of one of the files is a part of the line of the second file.

A part of the first file:

12319000    -64,7357668067227   -0,1111052148685535  
12319000    -79,68527661064425  -0,13231739777754026  
12319000    -94,69642857142858  -0,15117839559513543    
12319000    -109,59301470588237 -0,18277783185642743  
12319001    99,70264355742297   0,48329515727315125  
12319001    84,61113445378152   0,4060446341409862  
12319001    69,7032037815126    0,29803063228455073  
12319001    54,93886554621849   0,20958105041136763  
12319001    39,937394957983194  0,13623056582981297  
12319001    25,05574229691877   0,07748669438398018  
12319001    9,99716386554622    0,028110643107892755  

A part of the second file:

12319000.abf    mutant  1  
12319001.abf    mutant  2  
12319002.abf    mutant  3  

I need to create a file, where the line consists of this: all line from the first file and everything from the line of the second one. except for the filename in the first column.

As you can see, there are more, than one line in the first file, cooresponding to a line in the second one. I need that operation to be done with each line, so the output should be like this:

12319000    -94,69642857142858  -0,15117839559513543  mutant    1  
12319000    -109,59301470588237 -0,18277783185642743  mutant    1  
12319001    99,70264355742297   0,48329515727315125  mutant 2  
12319001    84,61113445378152   0,4060446341409862  mutant  2  

I’ve written this code:

oocytes = open(file_with_oocytes, 'r')  
results = open(os.path.join(path, 'results.csv'), 'r')  
results_new = open(os.path.join(path, 'results_with_oocytes.csv'), 'w')  
for line in results:  
    for lines in oocytes:  
        if lines[0:7] in line:  
            print line + lines[12:]  

But it prints out this, and nothing more, thow there are 45 line in the first file:

12319000    99,4952380952381    0,3011778623990699
    mutant  1  

12319000    99,4952380952381    0,3011778623990699
    mutant  2  

12319000    99,4952380952381    0,3011778623990699
    mutant  3  

What is wrong with the code?
Or it should be done somehow completely differently?

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

    Note that this solution doesn’t rely on the lengths of any field except for the length of the file extension in the second file.

    # make a dict keyed on the filename before the extension
    # with the other two fields as its value
    file2dict = dict((row[0][:-4], row[1:])  
                         for row in (line.split() for line in file2))
    
    # then add to the end of each row 
    # the values to it's first column
    output = [row + file2dict[row[0]] for row in (line.split() for line in file1)]
    

    For testing purposes only, I used:

    # I just use this to emulate a file object, as iterating over it yields lines
    # just use file1 = open(whatever_the_filename_is_for_this_data)
    # and the rest of the program is the same
    file1 = """12319000    -64,7357668067227   -0,1111052148685535
    12319000    -79,68527661064425  -0,13231739777754026
    12319000    -94,69642857142858  -0,15117839559513543
    12319000    -109,59301470588237 -0,18277783185642743
    12319001    99,70264355742297   0,48329515727315125
    12319001    84,61113445378152   0,4060446341409862
    12319001    69,7032037815126    0,29803063228455073
    12319001    54,93886554621849   0,20958105041136763
    12319001    39,937394957983194  0,13623056582981297
    12319001    25,05574229691877   0,07748669438398018
    12319001    9,99716386554622    0,028110643107892755""".splitlines()
    
    # again, use file2 = open(whatever_the_filename_is_for_this_data)
    # and the rest of the program will work the same
    file2 = """12319000.abf    mutant  1
    12319001.abf    mutant  2
    12319002.abf    mutant  3""".splitlines()
    

    where you should just use normal file objects. The output for the test data is :

       [['12319000', '-64,7357668067227', '-0,1111052148685535', 'mutant', '1'],
        ['12319000', '-79,68527661064425', '-0,13231739777754026', 'mutant', '1'],
        ['12319000', '-94,69642857142858', '-0,15117839559513543', 'mutant', '1'],
        ['12319000', '-109,59301470588237', '-0,18277783185642743', 'mutant', '1'],
        ['12319001', '99,70264355742297', '0,48329515727315125', 'mutant', '2'],
        ['12319001', '84,61113445378152', '0,4060446341409862', 'mutant', '2'],
        ['12319001', '69,7032037815126', '0,29803063228455073', 'mutant', '2'],
        ['12319001', '54,93886554621849', '0,20958105041136763', 'mutant', '2'],
        ['12319001', '39,937394957983194', '0,13623056582981297', 'mutant', '2'],
        ['12319001', '25,05574229691877', '0,07748669438398018', 'mutant', '2'],
        ['12319001', '9,99716386554622', '0,028110643107892755', 'mutant', '2']]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 3 byte arrays in C# that I need to combine into one.
I need to read the first n lines of a text file as lines
I need to create and combine several expressions for child entity into one to
I need to extract data from a .mpp file on the network and combine
Given two separate emacs buffers, how can I combine them by joining the first
Using Ruby 1.9.2 I need to parse a CSV file, and output lines with
What's the best way to combine two csv files and append the results to
I have two set of folders that have files with the same filenames and
I have two text files bala.txt and bala1.txt bala.txt contains text line by line
I need to combine two instances of a model, and the process I need

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.