SET
I have a csv file
which includes the current balance of my refreshments (it is comma separated, but in this example commas ,
are removed for improved readability):
NAME AMOUNT PRICE
Coca-Cola 8 1.25
Fanta 6 1.29
Dr. Pepper 2 2.20
Sprite 10 1.35
Guarana 6 1.80
Pepsi 4 1.25
I read all the data into the memory (list) and do all the necessary editing using:
import csv
# format list for refreshments
items_list = []
with open("my_refresments.csv", newline='') as fileOpener:
open_csv = csv.reader(fileOpener)
for rows in open_csv:
items_list.append(rows)
The list 'items_list' now prints out as:
items_list = [["NAME","AMOUNT","PRICE"],
["Coca-Cola","8","1.25"],
["Fanta","6","1.29"],
["Dr. Pepper","2","2.20"],
["Sprite","10","1.35"],
["Guarana","6","1.80"],
["Pepsi","4","1.25"]]
QUESTION
What gives me this kind of sorting:
# Notice that this is sorted, but items_list[0] is at it's place
items_list = [["NAME","AMOUNT","PRICE"],
["Coca-Cola","8","1.25"],
["Fanta","6","1.29"],
["Dr. Pepper","2","2.20"],
["Sprite","10","1.35"],
["Guarana","6","1.80"],
["pepsi","4","1.25"]]
Everything except first row items_list[0]
should stay unsorted ergo untouched where it stands.
Sorting the list
Generally a.sort()
is a good option with nested lists because it will not sort all the individual items inside the lists, but instead it sorts the lists compared one to another. In this case I can't use items_list.sort()
, because it will include also the headers in csv file, which I want to stay just where it is already located: items_list[0]
. Here is what happens when using items_list.sort()
:
items_list.sort()
[['Coca-Cola', '8', '1.25'],
['Fanta', '6', '1.29'],
['Dr. Pepper', '2', '2.20'],
['Sprite', '10', '1.35'],
['Guarana', '6', '1.80'],
['NAME', 'AMOUNT', 'PRICE'],
['Pepsi', '4', '1.25']]
Strangely, if I write all the refreshments with lower case
, it will work, because TEXT > text
, but I don't want to do that. I would like to use slice
to exclude the first row (headers), but it doesn't seem to have any effect what so ever (or maybe I'm doing it wrong):
# This doesn't do anything
items_list[1:].sort()
One possible solution
What I could do is to
- first copy headers into another list in one way or another
temp_list = [items_list[0]]
- delete
items_list[0]
using thedel items_list[0]
statement - sort the list using
items_list.sort()
, and finally - insert headers into the sorted list's
index(0)
withitems_list.insert(0, temp_list)
like this:
def sort_csv_list_exclude_headers(file_name):
items_list = []
with open(file_name, newline='') as fileOpener:
open_csv = csv.reader(fileOpener)
for rows in open_csv:
items_list.append(rows)
temp_list = [items_list[0]]
del items_list[0]
items_list.sort()
items_list.insert(0, temp_list[0])
with open(file_name, "w") as fileWriter:
write_csv = csv.writer(fileWriter, lineterminator='\n')
write_csv.writerows(items_list)
sort_csv_list_exclude_headers("SODA_BALANCE.csv")
Actually this is pretty good and simple in general when using csv with > 1 000 000 rows of "refreshments".
Subquestion
Is there any other (more simple) method?