this is my csv file \"id\",\"name\",\"quantity\",\"price\" \"789\",\"kazoo\",\"1
ID: 3539855 • Letter: T
Question
this is my csv file
"id","name","quantity","price"
"789","kazoo","17","999"
"11111","trumpet","22","64999"
"22223","english horn","6","82500"
write a python program does this using functions:
1. Show all entries by name. Once selected, grab all entries, order them ASCII-betically by name, and display
them as in this (small) example. Note the column ordering, always ID/name/quantity/price.
ID name quantity price
############################################
22223 english horn 6 $825.00
789 kazoo 17 $9.99
11111 trumpet 22 $649.99
2. Show all entries by ID. This is the same as option 8, only the ordering is by the ID numbers. Here's the
same example, appropriately ordered for option 9:
ID name quantity price
############################################
789 kazoo 17 $9.99
11111 trumpet 22 $649.99
22223 english horn 6 $825.00
Explanation / Answer
#!/usr/local/bin/python2.7
import csv
import operator
data = csv.reader(open('input.csv','r'))
sorteddata = sorted(data,key=operator.itemgetter(1),reverse=False)
print "ID name quantity price"
print "############################################"
for id, name, quantity, price in sorteddata:
print id," ", name ," ", quantity," ",'$',price
sorteddata = sorted(sorteddata,key=operator.itemgetter(2),reverse=False)
print "ID name quantity price"
print "############################################"
for id, name, quantity, price in sorteddata:
print id," ", name ," ", quantity," ",'$',price
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.