Programmer Deer sharing a code

In case you have csv files and due to events or just life need to organize or move things to the cloud

A regular csv file with Python can convert to Python language and you can manipulate data

CSV looks like this
Rank,Country,Gold,Silver,Bronze,Total
1,United States,39,41,33,113
2,China,38,32,18,88
3,Japan,27,14,17,58
4,Great Britain,22,21,22,65
5,ROC,20,28,23,71
6,Australia,17,7,22,46
7,Netherlands,10,12,14,36
8,France,10,12,11,33
9,Germany,10,11,16,37
10,Italy,10,10,20,40


With dictwriter command in Python
import csv

medals_table = [
{'country': 'United States', 'gold': 39, 'silver': 41, 'bronze': 33, 'rank': 1},
{'country': 'China', 'gold': 38, 'silver': 32, 'bronze': 18, 'rank': 2},
{'country': 'Japan', 'gold': 27, 'silver': 14, 'bronze': 17, 'rank': 3},
{'country': 'Great Britain', 'gold': 22, 'silver': 21, 'bronze': 22, 'rank': 4},
{'country': 'ROC', 'gold': 20, 'silver': 28, 'bronze': 23, 'rank': 5},
{'country': 'Australia', 'gold': 17, 'silver': 7, 'bronze': 22, 'rank': 6},
{'country': 'Netherlands', 'gold': 10, 'silver': 12, 'bronze': 14, 'rank': 7},
{'country': 'France', 'gold': 10, 'silver': 12, 'bronze': 11, 'rank': 8},
{'country': 'Germany', 'gold': 10, 'silver': 11, 'bronze': 16, 'rank': 9},
{'country': 'Italy', 'gold': 10, 'silver': 10, 'bronze': 20, 'rank': 10},
]
def sort_key(d: dict) -> str:
return d['country']


columns = ['country', 'gold', 'silver', 'bronze', 'rank']

filename = 'country_medals.csv'
with open(filename,'w', encoding='utf-8', newline='') as output_file:
writer = csv.DictWriter(output_file, fieldnames=columns, extrasaction= 'ignore')
writer.writeheader()
# for row in medals_table:
# writer.writerow(row)
writer.writerows(sorted(medals_table, key= sort_key))


Where the hash tags are you can take off the last line and it does it a different way on a separate file
you load a csv file and then create a Python file to manipulate the csv file so two tabs, one a csv and the other py as in python. the PY python manipulates the csv and creates a new file with a py ending.

with dictwriter you can only sort by function and not just the sort command on a dictionary--quirky