I wrote the following code which extracts the info. of a file and orders it alphabetically based on its second column objects:
import csv
import operator
import sys
def re_sort(in_file='books.csv', out_file='books_sort.csv'):
data = csv.reader(open('books.csv', newline=''), delimiter=',')
header = next(data)
sortedlist = sorted(data, key=operator.itemgetter(1))
with open("books_sorted.csv", "w", newline='') as csvfile:
cvsWriter = csv.writer(csvfile, delimiter=',')
cvsWriter.writerow(header)
cvsWriter.writerows(sortedlist)
Whenever I try to run this code on the command line, it gives me the error TypeError: 'newline' is an invalid keyword argument for this function. Do you guys see reasons why this may be happening. The following if a structured version of the contents in the file:
Title, Author, Publisher, Year, ISBN-10, ISBN-13
Automate the..., Al Sweigart, No Sta..., 2015, 15932..., 978-15932...
Dive into Py..., Mark Pilgr..., Apress, 2009, 14302..., 978-14302...
"Python Cook..., "David Bea..., O'Reil..., 2013, 14493..., 978-14493...
Think Python..., Allen B. D..., O'Reil..., 2015, 14919..., 978-14919...
"Fluent Pyth..., Luciano Ra..., O'Reil..., 2015, 14919..., 978-14919...
open
built-in function got newline
keyword in python 3, once said that, I can presume you're running your script using python 2.
In order to solve your issue:
python3 myscript.py
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With