Write a Python program that sorts all lines alphabetically within a text file.The program will be given 2 command line arguments: the first specifies the path to the file to sort, and the second specifies the column number to use as the key for sorting the file.Columns are numbered starting from 1. You can assume columns in the file are separated by a space " ", and that the column number provided is a valid column in the file.If the value in one column is a duplicate, the program sorts the lines with the duplicate value in alphabetical order based on the contents of their entire lines from start to finish.You should write your solution in sort.py.
Question
Write a Python program that sorts all lines alphabetically within a text file.The program will be given 2 command line arguments: the first specifies the path to the file to sort, and the second specifies the column number to use as the key for sorting the file.Columns are numbered starting from 1. You can assume columns in the file are separated by a space " ", and that the column number provided is a valid column in the file.If the value in one column is a duplicate, the program sorts the lines with the duplicate value in alphabetical order based on the contents of their entire lines from start to finish.You should write your solution in sort.py.
Solution
The selected text is in English. Here is the Python program as per your request:
import sys
def sort_file(file_path, column):
with open(file_path, 'r') as file:
lines = file.readlines()
lines.sort(key=lambda line: line.split()[column - 1])
with open(file_path, 'w') as file:
file.writelines(lines)
if __name__ == "__main__":
file_path = sys.argv[1]
column = int(sys.argv[2])
sort_file(file_path, column)
This program works as follows:
-
It first opens the file specified by the first command line argument in read mode and reads all lines into a list.
-
It then sorts this list of lines. The key for sorting is the specified column of each line. Note that Python's list indices start at 0, so we subtract 1 from the column number.
-
After sorting the lines, it opens the file again in write mode and writes the sorted lines back to the file.
-
The main part of the program checks if it is being run as a script (as opposed to being imported as a module). If it is, it reads the command line arguments, converts the second argument to an integer, and calls the
sort_filefunction with these arguments.
Similar Questions
Open the file romeo.txt and read it line by line. For each line, split the line into a list of words using the split() method. The program should build a list of words. For each word on each line check to see if the word is already in the list and if not append it to the list. When the program completes, sort and print the resulting words in python sort() order as shown in the desired output.
Use a command to sort temp2.txt , remove duplicate lines, and save the result in output.txt .Describe what this file now contains.
Write a program for binary search. The input will be the sorted file obtained as output of insertion sort or selection sort.The number to be searched should be given from the keyboard.The output will be the index of the location where the number is found. If the file contains unique numbers then a single index will be returned. If the file contains non-unique or duplicate numbers then the index of the first number in the list has to be returned.
The following data is connected to a Sort tool. The data type for the column is V_String.The Sort tool is configured to sort on the Value column in Ascending order, with Use Dictionary Order unchecked. What is the output of the Sort tool? A.B.C.
Single File Programming QuestionProblem StatementEmma, a computer science student, is organizing a list of names for a project. She needs a program to input a set of names and arrange them in alphabetical order. She wants to write a program that takes the number of names (n) as input and then receives n names. The program should then arrange the names in alphabetical order and print the sorted list.Emma approached you for help, and you are tasked with creating a program to assist her in achieving this.Input format :The first line consists of an integer N, representing the number of names.The next N lines of input consist of a string in each line, representing the names.Output format :The output displays strings, representing the sorted names in alphabetical order, separated by a line.Refer to the sample outputs for the formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:2 ≤ N ≤ 103 ≤ Length of each name ≤ 14Sample test cases :Input 1 :5KarthickRamKumarUmaSeethaOutput 1 :KarthickKumarRamSeethaUmaInput 2 :8KlausCatherineeeTylerDamonElenaStefanBonnieElijahOutput 2 :BonnieCatherineeeDamonElenaElijahKlausStefanTylerInput 3 :10WilliamOliviaJamesEmmaLiamAvaBenjaminSophiaMiaShakespeareOutput 3 :AvaBenjaminEmmaJamesLiamMiaOliviaShakespeareSophiaWilliamInput 4 :2EveCharliePuthOutput 4 :CharliePuthEve
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.