working with csv files the output from this programbased on the sample csv file created earlier isstarting to read csv file she loves yousept want to hold your handdec cant buy me loveapr hard days nightjuly done reading the csv dictwriter class in many cases the first row of csv file contains set of names (or keysthat define the fields within the rest of the csv that is the first row gives meaning to the columns and the data held in the rest of the csv file it is therefore very useful to capture this information and to structure the data written to csv file or loaded from csv file based on the keys in the first row the csv dictwriter returns an object that can be used to write values into the csv file based on the use of such named columns the file to be used with the dictwriter is provided when the class is instantiated import csv with open('names csv'' 'newline=''as csvfilefieldnames ['first_name''last_name''result'writer csv dictwriter(csvfilefieldnames=fieldnameswriter writeheader(writer writerow({'first_name''john''last_name''smith''result }writer writerow({'first_name''jane''last_name''lewis''result' }writer writerow({'first_name''chris''last_name''davies''result }note that when the dictwriter is created list of the keys must be provided that are used for the columns in the csv file the method writeheader(is then used to write the header row out to the csv file the method writerow(takes dictionary object that has keys based on the keys defined for the dictwriter these are then used to write data out to the csv (note the order of the keys in the dictionary is not importantin the above example code the result of this is that new file called names csv is created which can be opened in excelof courseas this is csv file it can also be opened in plain text editor as well