Lecture 16 Revision: CSV Files Flashcards
What is CSV format?
CSV = Comma Seperated Values
CSV format is commonly used for files storing spreadsheets and databases
files containing data in CSV format can be opened and saved by Excel.
CSV format is held in a ASCII text file.
Each lines contains multiple values seperated by commas.
You can think of each CSV file contains a table with multiple columns and rows.
Typically the first row contains column header names seperated by commas, but header lines are not compulsory.
What terms would a programmer think of when referring to CSV files?
They refer to FILES, LINES and VALUES
What terms would a programmer think of when referring to spreadsheets?
They refer to TABLES, ROWS, COLUMNS, HEADERS & ITEMS.
What terms would a programmer think of when referring to databases?
They refer to RECORDS and DATA
What terms would a programmer think of when referring to Python programming
They refer to LISTS and ELEMENTS
What module do you need to import to handle CSV files?
import csv
what does this function do?
def read_and_display_csv(filename):
with open(filename, mode=’r’) as file:
Opens the file in read mode.
Then sets up the alias file.
What does csv.reader(file) do?
The function .reader reads the file and creates and object containing the CSV files.
Can assign this to a variable, e.g. csv_reader = csv.reader(file)
How do we access specific values from CSV format tables? (Using Indexes)
[0][0] = row 0, column 0 (i.e the first item in the first row.
[0][1] = row 0, column 1 (i.e. the second item in the first row).
[3][2] = row 3, column 2 (i.e. the third item in the 4th row).
If only one number is used in the index then it refers to that whole row e.g. [8] = all of row 8 (the 9th row)
How do you turn the list of strings into a single string (i.e. concatenate them)?
Use method join()
Turns the list of strings into a single string.
This will join the strings in the list with a | seperator.
Join is therefore a method for the string object. The join method takes the headers as an argument. The method concatenates the objects string attribute and the strings in the argument. The join method then joins the single string.
What is an iterable?
If something is iterable then it means that it can return it’s rows one-by-one.
What are the two ways to manipulate CSV data?
- Using LISTS
- Using DICTIONARIES
How does the function reader() work?
The reader() function from the csv module is used to read data from CSV files.
It treats each line in the CSV file as a list of strings, with each string corresponding to a field (cell) in the row.
It works by:
1. Initialisation: Create a reader object by passsing a file object (opened in read mode) to reader()
- Iteration: Iterate over the reader() object to access each row in the CSV file. Each row is returned as a LIST.
What does function .Dictreader() do?
Instead of using the function reader() we can use Dictreader(). This function returns an iterable object containing the rows from the CSV file.
The object can then be assigned to a variable. The variable is then used to hold the rows from the CSV file as a DICTIONARY.
A for loop can then be used to iterate over each row in turn. The dictionary KEYS are the headers, and the VALUES are the data from the row.
What are the 2 main advantages of using Dictreader() over reader()?
- If a script is accessing individual values, then keys are easier to read than indices. e.g. it is easier the read and remember a header called ‘Eye Colour’ than index 6.
- If the columns in a CSV file change, then keys are more relaible than indices. e.g. if the height column is deleted then the ‘Eye Colour’ column does not need to be changed, but the index must be changed to 5.
How can you write to csv files in Python?
The function .Dictwriter (from the csv module function in Python is a tool used to write data to CSV files, where each row is represented as a dictionary with keys corresponding to column headers.
How does the function Dictwriter() work?
- Initialisation: Create a Dictwriterr object by passing it a file object (opened in write mode) with a list of column headers.
- Write Header: Use the writeheader() method to write the header (collumn names) to the CSV file
- Write Rows: Use the writerow() method. You need to provide the data in each row as a dictionary.