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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:44:21+00:00 2026-06-14T13:44:21+00:00

I have combined two .txt files using a program i wrote. Now once i

  • 0

I have combined two .txt files using a program i wrote. Now once i combined these two .txt files i was left with an unsorted combination of data.

my data looks like this

Bud Abbott 51 92.3
Mary Boyd 52 91.4
Hillary Clinton 50 82.1
Don Adams 51 90.4
Jill Carney 53 76.3
Randy Newman 50 41.2

however i want it to look like this

 ['Bud', 'Abbott', 51, 92.3]
 ['Don', 'Adams', 51, 90.4]
 ['Mary', 'Boyd', 52, 91.4]
 ['Jill', 'Carney', 53, 76.3]
 ['Hillary', 'Clinton', 50, 82.1]
 ['Randy', 'Newman', 50, 41.2]

how do i transform what i have into what i want

by the way the code i have to get my first set of data looks like this

def ReadAndMerge():

     library1=input("Enter 1st filename to read and merge:")
     with open(library1, 'r') as library1names:
          library1contents = library1names.read()
     library2=input("Enter 2nd filename to read and merge:")
     with open(library2, 'r') as library2names:
          library2contents = library2names.read()

     print(library1contents)
     print(library2contents)
     combined_contents = library1contents + library2contents  # concatenate text



     file = combined_contents
     lines = []

     for l in file:
          line = l.strip().split(" ")
          lines.append(line)


     lines.sort(key=lambda lines: lines[1])

     for line in lines:
          print(line)

print(combined_contents)
return(combined_contents)

ReadAndMerge()

and library 1 and library 2 look like this respectively

Bud Abbott 51 92.3
Mary Boyd 52 91.4
Hillary Clinton 50 82.1



Don Adams 51 90.4
Jill Carney 53 76.3
Randy Newman 50 41.2

How do i get it to be sorted and in a list

I updated my program with your new lines, but when i run it i keep getting this error

Traceback (most recent call last):
 File "C:/Users/Shane/Documents/Amer CISC/readandmerge.py", line 30, in <module>
ReadAndMerge()
 File "C:/Users/Shane/Documents/Amer CISC/readandmerge.py", line 23, in ReadAndMerge
lines.sort(key=lambda lines: lines[1])
 File "C:/Users/Shane/Documents/Amer CISC/readandmerge.py", line 23, in <lambda>
lines.sort(key=lambda lines: lines[1])
 IndexError: list index out of range
 >>> 
  • 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-14T13:44:22+00:00Added an answer on June 14, 2026 at 1:44 pm
    def getListOfLinesInFile(file): 
        file = open(file, "r")
        lines = []    
        for l in file:
            line = l.strip().split(" ")
            lines.append(line)
        file.close()
        return lines
    

    To use it in your program, just your ReadAndMerge -function with:

    def ReadAndMerge():
        library1 = input("Enter 1st filename to read and merge: ")
        library1_list = getListOfLinesInFile(library1)
    
        library2 = input("Enter 2nd filename to read and merge: ")
        library2_list = getListOfLinesInFile(library1)
    
        library_merged = library1_list + library2_list
        library_merged.sort(key=lambda library_merged: library_merged[1])
    
        print(*library_merged)
        return library_merged
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have combined two arrays using array_merge, and now need to sort them alphabetically.
I have combined two tables using union all . However, the produced data has
I have two txt files that contain several names and addresses or phone numbers.
I have 4 text files in one folder and a pattern.txt to compare these
I have two text files bala.txt and bala1.txt bala.txt contains text line by line
I have three files G_P_map.txt, G_S_map.txt and S_P_map.txt. I have to combine these three
I have combined these two queries: SELECT `ad_general`.`id`, `ad_general`.`status`, `ad_general`.`category`, `ad_general`.`type`, `ad_general`.`specification`, `ad_general`.`m2`, `ad_general`.`price`,
Scenario I have two WCF Services combined in a single App.Config file. I can't
I have two PNGs that I am trying to combine into a PDF using
I have data from two different sources that I need to combine. Some data

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.