CSV → 2D List
Reading a CSV File into a 2 Dimensional Python List
Sometimes you will need to read data stored in comma separated value (CSV) files. You can use the csv module in order to achieve this.
CSV files usually use a comma as the delimiter between each data item, but sometimes might use a tab or other character instead.
2D List → CSV
Writing a 2 Dimensional List to a CSV File
Often you will want to be able to save 2 Dimensional List data to a CSV file so that it isn’t deleted when your program exits. you can use csv writer to achieve this.
CSV → Dictionary
Reading a CSV File into a Python Dictionary
Sometimes you want to read the data into a dictionary instead of a 2 Dimensional list. CSV reader can do this too.
Dictionary → CSV
Writing a Python Dictionary to a CSV File
Challenge 37 – Animal Stats Printer
- Read the CSV file animal_data.csv into a 2 dimensional list and print out the items.
- Print out the list in order of population
- Ask the user to type in a column selection(e.g. “average age”) and have the program sort the items in that order.
Challenge 38 – Animal Stats Adder
- Write a program that allows you to add an extra animal to the animal_facts.csv file. (you might want to use append ‘a’ mode instead of write ‘w’ mode.)
- Allow the user to keep adding more animals until the type an empty string.
- Once the user has finished adding, print out the animals in alphabetical order.
Challenge 39 – IT Support Log
Ask the user to input their name and a description of their IT issue. Save the issue to the file ‘ict_issue_log.txt’
Adapt your program so that it automatically adds the current date and time to the log.
Adapt your program so that each ticket is given a unique job number that increments automatically.
Challenge 40 – Country Top Trumps
Create program that reads the country_csv file into a list of dictionaries.
Create a one player top trumps game where the player picks a single card and the computer picks a card and then they see who wins.
Extend the game so that you play a full game of top trumps with a deck of card and two players