Knowee
Questions
Features
Study Tools

SortWrite 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.Example output$ cat students.txtMicheline Lalevee [email protected] USYDRobyn Maes [email protected] USYDVenus Kinsley [email protected] USYDDevonne Berrygun [email protected] UNSWLuci Broad [email protected] UTSMorissa Bolino [email protected] MacquarieDanika Heatley [email protected] WSURichmond Goodnow [email protected] USYD$ python3 sort.py students.txt 2Devonne Berrygun [email protected] UNSWMorissa Bolino [email protected] MacquarieLuci Broad [email protected] UTSRichmond Goodnow [email protected] USYDDanika Heatley [email protected] WSUVenus Kinsley [email protected] USYDMicheline Lalevee [email protected] USYDRobyn Maes [email protected] USYDError handlingIf the user does not provide exactly 2 command line arguments, print Error: must provide exactly 2 arguments to stderr, and exit the programIf the file does not exist, print Error: '<filename>' cannot be found to stderr, and exit the programYou may assume that all files provided to the program are valid text files, and the user has read permissions on all input files.Note: Your coding style will be examined, so take this into consideration for your solution.Be careful when sorting a column with numbers – sorting numbers alphabetically is different to sorting numbers numerically!You may only use the sys module – no other imports are allowed for this task.

Question

SortWrite 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.Example outputcatstudents.txtMichelineLaleveemlalevee0@spiegel.deUSYDRobynMaesrmaes1@admin.chUSYDVenusKinsleyvkinsley2@tonline.deUSYDDevonneBerrygundberrygun3@howstuffworks.comUNSWLuciBroadlbroad4@technorati.comUTSMorissaBolinombolino5@sciencedaily.comMacquarieDanikaHeatleydheatley6@reuters.comWSURichmondGoodnowrgoodnow7@google.comUSYD cat students.txtMicheline Lalevee [email protected] USYDRobyn Maes [email protected] USYDVenus Kinsley [email protected] USYDDevonne Berrygun [email protected] UNSWLuci Broad [email protected] UTSMorissa Bolino [email protected] MacquarieDanika Heatley [email protected] WSURichmond Goodnow [email protected] USYD python3 sort.py students.txt 2Devonne Berrygun [email protected] UNSWMorissa Bolino [email protected] MacquarieLuci Broad [email protected] UTSRichmond Goodnow [email protected] USYDDanika Heatley [email protected] WSUVenus Kinsley [email protected] USYDMicheline Lalevee [email protected] USYDRobyn Maes [email protected] USYDError handlingIf the user does not provide exactly 2 command line arguments, print Error: must provide exactly 2 arguments to stderr, and exit the programIf the file does not exist, print Error: '<filename>' cannot be found to stderr, and exit the programYou may assume that all files provided to the program are valid text files, and the user has read permissions on all input files.Note: Your coding style will be examined, so take this into consideration for your solution.Be careful when sorting a column with numbers – sorting numbers alphabetically is different to sorting numbers numerically!You may only use the sys module – no other imports are allowed for this task.

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

所选择的文本是英语。

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 function sort_records(csv_filename, new_filename) that sorts the records of a CSV file and writes the results as a new CSV file. The first column of the CSV file will be the city name. The rest of the columns will be months of the year. The first row of the CSV file will take the form of the column headings, with all columns other than the first being months of the year. Here is an example file fragment:max_temp.csvcity/month,Jan,Feb,Mar,AprMelbourne,41.2,35.5,37.4,29.3Brisbane,31.3,40.2,37.9,29Darwin,34,34,33.2,34.5Note that your code will be tested over different CSV files, with different ranges of months in them. Irrespective of the exact months contained in the file, you may assume that the city name will always be in the first column, and the months in the remaining columns.You must sort the data in alphabetical order according to the city name (stored in the first column). Your program should write the sorted records to a new file with the name given by the argument new_filename.Here is an example of how sort_records() should work. 'program.py' is the program and below is its terminal output.sort_records('max_temp.csv', 'sorted.csv')result = open('sorted.csv')print(result.read())result.close()city/month,Jan,Feb,Mar,AprBrisbane,31.3,40.2,37.9,29Darwin,34,34,33.2,34.5Melbourne,41.2,35.5,37.4,29.3​Note that the row for Melbourne has been sorted below the rows for Brisbane and Darwin because Melbourne comes after Brisbane and Darwin, based on alphabetical ordering.

What will the following code do?df.sort_values(by='ColumnName', ascending=False, inplace=True)dfA.Sort the df pandas DataFrame in descending order.B.Sort the df pandas DataFrame in ascending order.C.Sort the df pandas DataFrame in descending order, and force the change to take place in memory.

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.

1/1

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.