Question: Write a program re_order.py that when run on the command line, takes 2 file names; an input csv file and output file name. It opens
Write a program re_order.py that when run on the command line, takes 2 file names; an input csv file and output file name. It opens the input CSV file, reads it and writes out the data with the first and second columns switched.
This program should have a function re_order 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.
Please note that this program/function must work for any input csv file that has at least 2 columns, not just the one provided for your testing purposes. You need not worry about checking for invalid files. For example, it should also work on the colors_commas.csv file created with the tabs_to_commas.py program.
You may assume that the input file will use the default delimiter (comma) and will always have at least 2 columns. Hint: It's specifically for csv files, so use the csv module. Also, it might help to review how slicing works.
A file books.csv is provided to use as a test file.
$ python re_order.py books.csv books_author.csv
Results in the creation of a new file books_author.csv that contains the same data as in books.csv, with the first two columns (title and author) switched.
>>> from re_order import re_order >>> re_order(in_file='books.csv', out_file='books_author.csv')
Will also result in the creation of a new file books_author.csv . The first 2 lines of books_author.csv should be:
Author,Title,Publisher,Year,ISBN-10,ISBN-13 Al Sweigart,Automate the Boring Stuff with Python,No Starch Press,2015,1593275994,978-1593275990
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
