I have devices.txt file that looks like this:
tw004:Galaxy S5:Samsung:Mobilni telefon:5
tw002:Galaxy S6:Samsung:Mobilni telefon:1
tw001:Huawei P8:Huawei:Mobilni telefon:4
tw003:Huawei P9:Huawei:Mobilni telefon:3
Now, I have code like this, and I have to chose how to sort devices in table (for example sort them by code from tw001 to tw004 or sort them by producer's name from A to Z)
def formatheader():
print(
"Code | Name | Producer | Description | Quantity |\n"
"-----+------------+------------+-------------------------+-------------|")
def sortbycode():
devices = open('devices.txt', 'r')
formatheader()
for i in devices:
devices = i.strip("\n").split(":")
print("{0:5}|{1:13}|{2:15}|{3:18}|{4:5}".format(
devices[0],
devices[1],
devices[2],
devices[3],
devices[4]))
print()
How to do that?
sorted()(with no keys, splitting, etc.), you'll have the list sorted by devices.