Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a program re_sort.py that when run on the command line, takes a filename of a CSV file as input, then reads the CSV file,

Create a program re_sort.py that when run on the command line, takes a filename of a CSV file as input, then reads the CSV file, sorts the file by the values of the second column, and writes the data to a new CSV file with _sort appended to the file name. So if the input filename is books.csv , the output file should be books_sort.csv . Assume the CSV file has 1 single header row.

Think about what might be a useful coding thing to use for a CSV file when we know there is a header row in the file. This program should have a function re_sort that has two arguments: in_file , the name of the csv file to read, and

out_file , the name of the csv file to create, so it can be used from the REPL or another Python module. Hint: It's specifically for csv files, so use the csv module.

Please note that this program/function must work for any input csv file that has a header row and at least 2 columns, not just the one we use for an example. You need not worry about checking for invalid files.

$ python re_sort.py books.csv

Will result in a file named books_sort.py .

>>> from re_sort import re_sort >>> re_sort(in_file='books.csv', out_file='books_sort.csv')

Will also result in a file named books_sort.py . The last 2 lines of books_sort.csv should be:

"Fluent Python: Clear, Concise, and Effective Programming",Luciano Ramalho,O'Reilly Media,2015,1491946008,978-1491946008 Dive into Python 3,Mark Pilgrim,Apress,2009,1430224150,978-1430224150

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essential SQLAlchemy Mapping Python To Databases

Authors: Myers, Jason Myers

2nd Edition

1491916567, 9781491916568

More Books

Students also viewed these Databases questions