Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm trying to make a program called re_order.py that when run on the command line, takes 2 file names; an input csv file and output

I'm trying to make a program called "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.

I can't use DictReader/DictWriter.

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.

A file books.csv is used as a test file.

books.csv:

Title,Author,Publisher,Year,ISBN-10,ISBN-13 Automate the Boring Stuff with Python,Al Sweigart,No Starch Press,2015,1593275994,978-1593275990 Dive into Python 3,Mark Pilgrim,Apress,2009,1430224150,978-1430224150 "Python Cookbook, Third edition","David Beazley, Brian K Jones",O'Reilly Media,2013,1449340377,978-1449340377 Think Python: How to Think Like a Computer Scientist,Allen B. Downey,O'Reilly Media,2015,1491939362,978-1491939369 "Fluent Python: Clear, Concise, and Effective Programming",Luciano Ramalho,O'Reilly Media,2015,1491946008,978-1491946008 

$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 4 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 Mark Pilgrim,Dive into Python 3,Apress,2009,1430224150,978-1430224150 "David Beazley, Brian K Jones","Python Cookbook, Third edition",O'Reilly Media,2013,1449340377,978-1449340377 

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

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions