DATA STRUCTURE AND ALGORITHM USING PYTHON

Similar documents
Data Structure. IBPS SO (IT- Officer) Exam 2017

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

12 Abstract Data Types

CS 171: Introduction to Computer Science II. Binary Search Trees

Binary Trees

CS301 - Data Structures Glossary By

Data Abstractions. National Chiao Tung University Chun-Jen Tsai 05/23/2012

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

Algorithms and Data Structures

Data Structures I: Linked Lists

Basic Data Structures (Version 7) Name:

1 Binary trees. 1 Binary search trees. 1 Traversal. 1 Insertion. 1 An empty structure is an empty tree.

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

4.1 COMPUTATIONAL THINKING AND PROBLEM-SOLVING

Summer Final Exam Review Session August 5, 2009

Data structure and algorithm in Python

Trees. (Trees) Data Structures and Programming Spring / 28

Fundamentals of Data Structure

Chapter 5 Data Structures Algorithm Theory WS 2016/17 Fabian Kuhn

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

Overview of Presentation. Heapsort. Heap Properties. What is Heap? Building a Heap. Two Basic Procedure on Heap

Recursive Data Structures and Grammars

Name CPTR246 Spring '17 (100 total points) Exam 3

Tables, Priority Queues, Heaps

Learning Goals. CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues. Today s Outline. Back to Queues. Priority Queue ADT

CS24 Week 8 Lecture 1

Data Structures Question Bank Multiple Choice

Binary Trees. Height 1

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

Algorithms. Deleting from Red-Black Trees B-Trees

Course Review. Cpt S 223 Fall 2009

Cpt S 223 Fall Cpt S 223. School of EECS, WSU

DATA STRUCTURES AND ALGORITHMS

Programming II (CS300)

Department of Computer Science and Technology

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Chapter 9 Abstract Data Types and Algorithms

Introduction to Indexing 2. Acknowledgements: Eamonn Keogh and Chotirat Ann Ratanamahatana

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

CS 270 Algorithms. Oliver Kullmann. Binary search. Lists. Background: Pointers. Trees. Implementing rooted trees. Tutorial

Cpt S 122 Data Structures. Data Structures

Stackless BVH Collision Detection for Physical Simulation

Computer Science 9608 (Notes) Chapter: 4.1 Computational thinking and problem-solving

Course Review. Cpt S 223 Fall 2010

CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL

COMP 103 RECAP-TODAY. Priority Queues and Heaps. Queues and Priority Queues 3 Queues: Oldest out first

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS

Stacks, Queues and Hierarchical Collections

[ DATA STRUCTURES ] Fig. (1) : A Tree

B-Trees and External Memory

Lecture Notes for Advanced Algorithms

Data Structure. A way to store and organize data in order to support efficient insertions, queries, searches, updates, and deletions.

Trees. Tree Structure Binary Tree Tree Traversals

B-Trees and External Memory

CS8391-DATA STRUCTURES QUESTION BANK UNIT I

Question Bank Subject: Advanced Data Structures Class: SE Computer

Red-Black trees are usually described as obeying the following rules :

CS 1114: Implementing Search. Last time. ! Graph traversal. ! Two types of todo lists: ! Prof. Graeme Bailey.

LECTURE 11 TREE TRAVERSALS

Data Structures and Algorithms

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

What we already know. more of what we know. results, searching for "This" 6/21/2017. chapter 14

INSTITUTE OF AERONAUTICAL ENGINEERING

ECE 242 Data Structures and Algorithms. Trees IV. Lecture 21. Prof.

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

An Introduction to Trees

Chapter 8: Data Abstractions

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

Chapter 20: Binary Trees

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Tree-Structured Indexes

An AVL tree with N nodes is an excellent data. The Big-Oh analysis shows that most operations finish within O(log N) time

Course Review for Finals. Cpt S 223 Fall 2008

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

CSE 230 Intermediate Programming in C and C++

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

CSC148H Week 3. Sadia Sharmin. May 24, /20

Chapter 8: Data Abstractions

Trees. CSE 373 Data Structures

Programming II (CS300)

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

Extra: B+ Trees. Motivations. Differences between BST and B+ 10/27/2017. CS1: Java Programming Colorado State University

3137 Data Structures and Algorithms in C++

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

Lecture 10 Notes Linked Lists

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

Dynamic Storage Allocation

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

Do you remember what is the most powerful tool for managing complexity?

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

DDS Dynamic Search Trees

Tree-Structured Indexes

ME/CS 132: Advanced Robotics: Navigation and Vision

Queues. ADT description Implementations. October 03, 2017 Cinda Heeren / Geoffrey Tien 1

Transcription:

DATA STRUCTURE AND ALGORITHM USING PYTHON Advanced Data Structure and File Manipulation Peter Lo

Linear Structure Queue, Stack, Linked List and Tree 2

Queue A queue is a line of people or things waiting to be handled, usually in sequential order starting at the beginning or top of the line or sequence. In computer technology, a queue is a sequence of work objects that are waiting to be processed. 3

Queue (Cont.) Queue has two main operations in data structure: Enqueue: append an element to the tail of the queue Dequeue: remove an element from the head of the queue With a queue you remove the item least recently added (First-In First-Out / FIFO) 4

Implement Queue by List It s possible to use a regular list as a queue but this is not ideal from a performance perspective. Lists are quite slow for this purpose because inserting or deleting an element at the beginning requires shifting all of the other elements by one. Using a list as a makeshift queue in Python is not recommend unless you re dealing with a small number of elements only. 5

Implement Queue by List 6

Implement Queue by collections.deque The deque class implements a double-ended queue that supports adding and removing elements from either end. Python s deque objects are implemented as doubly-linked lists which gives them excellent performance for enqueuing and dequeuing elements, but poor performance for randomly accessing elements in the middle of the queue. Because deques support adding and removing elements from either end equally well, they can serve both as queues and as stacks. collections.deque is a great default choice if you re looking for a queue data structure in Python s standard library. 7

Implement Queue by collections.deque 8

Implement Queue by queue.queue This queue implementation in the Python standard library is synchronized and provides locking semantics to support multiple concurrent producers and consumers. The queue module contains several other classes implementing multi-producer, multi-consumer queues that are useful for parallel computing. Depending on your use case the locking semantics might be helpful, or just incur unneeded overhead. In this case you d be better off with using collections.deque as a general purpose queue. 9

Implement Queue by queue.queue 10

Stack A Stack is an ordered collection of items where the addition of new items and the removal of existing items always takes place at the same end. This end is commonly referred to as the top. The end opposite the top is known as the base 11

Stack (Cont.) Stack has two main operations in data structure: Push: append an element on top of the stack Pop: remove an element from the top of the stack With a stack you remove the item most recently added (Last-In First-Out / LIFO). 12

Implement Stack by List Python s built-in list type makes a decent stack data structure as it supports push and pop operations in amortized time. Python s lists are implemented as dynamic arrays internally which means they occasional need to resize the storage space for elements stored in them when elements are added or removed. The list over-allocates its backing storage so that not every push or pop requires resizing and you get an amortized time complexity for these operations. The downside is that this makes their performance less consistent than the stable inserts and deletes provided by a linked list based implementation. On the other hand lists do provide fast time random access to elements on the stack which can be an added benefit. 13

Implement Stack by List 14

Implement Stack by collections.deque The deque class implements a double-ended queue that supports adding and removing elements from either end in non-amortized time. Because deques support adding and removing elements from either end equally well, they can serve both as queues and as stacks. Python s deque objects are implemented as doubly-linked lists which gives them excellent and consistent performance for inserting and deleting elements, but poor performance for randomly accessing elements in the middle of the stack. collections.deque is a great choice if you re looking for a stack data structure in Python s standard library with the performance characteristics of a linked-list implementation. 15

Implement Stack by collections.deque 16

Implement Stack by queue.lifoqueue This stack implementation in the Python standard library is synchronized and provides locking semantics to support multiple concurrent producers and consumers. The queue module contains several other classes implementing multi-producer, multi-consumer queues that are useful for parallel computing. Depending on your use case the locking semantics might be helpful, or just incur unneeded overhead. In this case you d be better off with using a list or a deque as a general purpose stack. 17

Implement Stack by queue.lifoqueue 18

Linked List A linked list is a sequence of data elements, which are connected together via links. Each data element contains a connection to another data element in form of a pointer. A linked list consists of nodes Each node consists of a value to another node and a pointer 19

Why use Linked Lists? Advantage Disadvantage A linked list saves memory Linear look up time It only allocates the memory required for values to be stored. Linked list nodes can live anywhere in the memory. Each linked list node can be flexibly moved to a different address. When looking for a value in a linked list, you have to start from the beginning of chain, and check one element at a time for a value you re looking for. 20

Node Class The basic building block for linked list implementation is node. Each node object must hold at least two pieces of information: The node must contain the list item itself. We call this the data field of the node. Each node must hold a reference to the next node. To construct a node, you need to supply the initial data value for the node. The Node class also includes the usual methods to access and modify the data and the next reference 21

Node Class (Cont.) The special Python reference value None play an important role in the Node class and linked list. A reference to None will denote the fact that there is no next node. Note in the constructor that a node is initially created with next set to None. Since this is sometimes referred to as grounding the node, we will use the standard ground symbol to denote a reference that is referring to None. It is always a good idea to explicitly assign None to your initial next reference values 22

Linked List Class The linked list will be built from a collection of nodes, each linked to the next by explicit references. Once we find the first node, each item after that can be found by successively following the next links. The Linked List class must maintain a reference to the first node. Note that each list object will maintain a single reference to the head of the list. Initially when we construct a list, there are no items. 23

Add Node to Linked List Each item of the list must reside in a node object. Then creates a new node and places the item as its data. Now we must complete the process by linking the new node into the existing structure. 1. Changes the next reference of the new node to refer to the old first node of the list. Now that the rest of the list has been properly attached to the new node. 2. Modify the head of the list to refer to the new node. 24

Add Node to Linked List (Cont.) Line 2 creates a new node and places the item as its data. Now we must complete the process by linking the new node into the existing structure. Line 3 changes the next reference of the new node to refer to the old first node of the list. Now that the rest of the list has been properly attached to the new node, we can modify the head of the list to refer to the new node. The assignment statement in line 4 sets the head of the list. 25

Linked List Traversal Linked List traversal is the process of systematically visiting each node. We use an external reference that starts at the first node in the list. As we visit each node, we move the reference to the next node by traversing the next reference. 26

Count the List Size To implement the size method, we need to traverse the linked list and keep a count of the number of nodes that occurred. The external reference is called current and is initialized to the head of the list in line 2. At the start of the process we have not seen any nodes so the count is set to 0. Lines 4 6 actually implement the traversal. As long as the current reference has not seen the end of the list (None), we move current along to the next node via the assignment statement in line 6. Again, the ability to compare a reference to None is very useful. Every time current moves to a new node, we add 1 to count. Finally, count gets returned after the iteration stops. 27

Print the List Node To implement the print method, we need to traverse the linked list and keep a count of the number of nodes that occurred. The external reference is called current and is initialized to the head of the list in line 2. Lines 3 5 actually implement the traversal. As long as the current reference has not seen the end of the list (None), we move current along to the next node via the assignment statement in line 5. Again, the ability to compare a reference to None is very useful. Every time current moves to a new node, we print the value. Finally, whole list printed out after the iteration stops. 28

Search in Linked List Searching for a value in a linked list implementation of an unordered list also uses the traversal technique. As we visit each node in the linked list we will ask whether the data stored there matches the item we are looking for. We may not have to traverse all the way to the end of the list. In fact, if we do get to the end of the list, that means that the item we are looking for must not be present. Also, if we do find the item, there is no need to continue. 29

Search in Linked List (Cont.) The traversal is initialized to start at the head of the list (line 2). We also use a Boolean variable called found to remember whether we have located the item we are searching for. Since we have not found the item at the start of the traversal, found can be set to False (line 3). The iteration in line 4 takes into account both conditions discussed above. As long as there are more nodes to visit and we have not found the item we are looking for, we continue to check the next node. The question in line 5 asks whether the data item is present in the current node. If so, found can be set to True. 30

Remove a Node in Linked List The remove method requires two logical steps. Traverse the list looking for the item that want to remove: starting with an external reference set to the head of the list, we traverse the links until we discover the item we are looking for. Remove the item once we find the item by modifying the link in the previous node so that it refers to the node that comes after current. 31

Remove a Node in Linked List (Cont.) Movement of previous and current as they progress down the list looking for the node containing the value 17. 32

Remove a Node in Linked List (Cont.) Lines 2 3 assign initial values to the two references. Note that current starts out at the list head as in the other traversal examples. previous, however, is assumed to always travel one node behind current. For this reason, previous starts out with a value of None since there is no node before the head. The Boolean variable found will again be used to control the iteration. In lines 6 7 we ask whether the item stored in the current node is the item we wish to remove. If so, found can be set to True. If we do not find the item, previous and current must both be moved one node ahead. Again, the order of these two statements is crucial. previous must first be moved one node ahead to the location of current. At that point, current can be moved. This process is often referred to as inch-worming as previous must catch up to current before current moves ahead. 33

Tree Structure Binary Tree 34

Tree Structure In computer science, a tree is a data structure that is modeled after nature. Unlike trees in nature, the tree data structure is upside down: the root of the tree is on top. A tree consists of nodes and its connections are called edges. The bottom nodes are also named leaf nodes. A tree may not have a cycle. 35

Binary Tree A binary tree is a data structure where every node has at most two children (left and right child). The root of a tree is on top. Every node below has a node above known as the parent node. 36

Terminology Term Path Root Parent Child Leaf Subtree Level Key Explanation The sequence of nodes along the edges of a tree The node at the top of the tree is called root. There is only one root per tree and one path from the root node to any node. Any node except the root node has one edge upward to a node called parent. The node below a given node connected by its edge downward is called its child node. The node which does not have any child node is called the leaf node. Subtree represents the descendants of a node. Level of a node represents the generation of a node. If the root node is at level 0, then its next child node is at level 1, its grandchild is at level 2, and so on. Key represents a value of a node based on which a search operation is to be carried out for a node. 37

Types of Binary Trees There are three type of Binary Tree 38

Full Binary Tree A Binary Tree is full if every node has 0 or 2 children. Following are examples of full binary tree. We can also say a full binary tree is a binary tree in which all nodes except leaves have two children. In a Full Binary tree, number of leaf nodes is number of internal nodes plus 1: L = I + 1 Where L = Number of leaf nodes, I = Number of internal nodes 39

Complete Binary Tree A Binary Tree is complete if all levels are completely filled except possibly the last level and the last level has all keys as left as possible 40

Perfect Binary Tree A Binary tree is Perfect Binary Tree in which all internal nodes have two children and all leaves are at same level. A Perfect Binary Tree of height h (where height is number of nodes on path from root to leaf) has 2h 1 node. 41

Degenerate Tree A degenerate (or pathological) tree is where each parent node has only one associated child node. This means that performance-wise, the tree will behave like a linked list data structure 42

Binary Search Tree Binary Search Tree is a data structure that quickly allows us to maintain a sorted list of numbers. The properties that separates a binary search tree from a regular binary tree is It is called a binary tree because each tree node has maximum of two children. All nodes of left subtree are less than root node All nodes of right subtree are more than root node Both subtrees of each node are also BST (i.e. they have the above two properties) 43

Searching in BST A recursive algorithm to search for a key in a BST follows immediately from the recursive structure: If the tree is empty, we have a search miss If the search key is equal to the key at the root, we have a search hit. Otherwise, we search recursively in the appropriate subtree. 44

Example ex 45

Binary Tree Traversal Traversal is a process that visits all the nodes in the tree. Since a tree is a nonlinear data structure, there is no unique traversal. Depth-First Traversal Pre Order Traversal visit the parent first and then left and right children In Order Traversal visit the left child, then the parent and the right child Post Order Traversal visit left child, then the right child and then the parent Breadth-First Traversal Level Order Traversal visits nodes by levels from top to bottom and from left to right 46

Example Consider the following tree and its traversals: Pre Order: 8, 5, 9, 7, 1, 12, 2, 4, 11, 3 In Order: 9, 5, 1, 7, 2, 12, 8, 4, 3, 11 Post Order: 9, 1, 2, 12, 7, 5, 3, 11, 4, 8 Level Order: 8, 5, 4, 9, 7, 11, 1, 12, 3, 2 47

Create Node Class A new class named Node with 3 attributes is create to represent a tree node: Left node Right node Node s data When you create a node, both left and right node equal to None. Let s create a tree node containing the value 8. 48

Insert a Tree Node We need a method to help us populate our tree. This method takes the node s data as an argument and inserts a new node in the tree. 49

Example The inset method is called recursively as we are locating the place where to add the new node. Detail Algorithm: Left child for root node "8" is None, attach the new node "3" to it. Right child for root node "8" is None, attach the new node "10" to it. Left child for root node "8" is not None, go to node "3" Left child for root node "3" is None, attach the new node "1" to it. 50

Using binarytree Library binarytree (https://pypi.org/project/binarytree/) is a Python library which provides a simple API to generate, visualize, inspect and manipulate binary trees. It allows you to skip the tedious work of setting up test data, and dive straight into practicing your algorithms. Heaps and Binary Search Tree (BST) are also supported. 51

Example 52

Example (Cont.) 53

File Manipulation Open and Read Text File 54

File There are two separate types of files that Python handles: Binary file Text files. Knowing the difference between the two is important because of how they are handled. 55

Binary File Most files that you use in your computer are binary files. For example, Microsoft Word.doc file is a binary file, even if it has text in it. Other examples of binary files include: Image files including.jpg,.png,.bmp,.gif, etc. Database files including.mdb,.frm, and.sqlite Documents including.doc,.xls,.pdf, and others. That s because these files all have requirements for special handling and require a specific type of software to open it. For example, you need Excel to open an.xls file 56

Text File A text file has no specific encoding and can be opened by a standard text editor without any special handling. Every text file must adhere to a set of rules: Text files have to be readable as is. They can contain a lot of special encoding, especially in HTML or other markup languages, but you ll still be able to tell what it says Data in a text file is organized by lines. In most cases, each line is a distinct element, whether it s a line of instruction or a command. Text files have some unseen character at the end of each line which lets the text editor know that there should be a new line. In Python, it is denoted by the \n. 57

Comma Separated Value (CSV) Text File A CSV file is a comma separated values file commonly used by spreadsheet programs such as Microsoft Excel. It contains plain text data sets separated by commas with each new line in the CSV file representing a new database row and each database row consisting of one or more fields separated by a comma. 58

Fixed-width Text File Data in a fixed-width text file is arranged in rows and columns, with one entry per row. Each column has a fixed width, specified in characters, which determines the maximum amount of data it can contain. No delimiters are used to separate the fields in the file. 59

Tab-Separated Values (TSV) Text File A Tab-Separated Values (TSV) file (also called tabdelimited file) is a simple text format for storing data in a tabular structure, e.g., database table or spreadsheet data, and a way of exchanging information between databases. Each record in the table is one line of the text file 60

Opening a File in Python In order to open a file for writing or use in Python, you must rely on the built-in open () function. The open() function takes two parameters; filename, and access_mode. The access_mode attribute of a file object tells you which mode a file was opened in. And the filename attribute tells you the name of the file that the file object has opened. 61

File Access Modes Mode r rb Function Open file for reading only. Starts reading from beginning of file. This default mode. Open a file for reading only in binary format. Starts reading from beginning of file. r+ Open file for reading and writing. File pointer placed at beginning of the file rb+ w wb Open a file for reading and writing in binary format. File pointer placed at beginning of the file Open file for writing only. File pointer placed at beginning of the file. Overwrites existing file and creates a new one if it does not exists. Same as w but opens in binary mode. w+ Same as w but also allows to read from file. wb+ a ab Same as wb but also allows to read from file. Open a file for appending. Starts writing at the end of file. Creates a new file if file does not exist. Same as a but in binary format. Creates a new file if file does not exist. a+ Same as a but also open for reading. ab+ Same as ab but also open for reading. 62

File Access Modes Summary Function r r+ w w+ a a+ Read Write Create new file if not exist Overwrite existing file Pointer place at beginning of file Point place at end of file 63

The File Object Attributes Once a file is opened and you have one file object, you can get various information related to that file. Here is a list of all attributes related to file object: Attribute file.closed file.mode file.name Description Returns true if file is closed, false otherwise. Returns access mode with which file was opened Returns name of the file. 64

Closing a File in Python When we are done with operations to the file, we need to properly close the file. Closing a file will free up the resources that were tied with the file and is done using Python close() method. Python has a garbage collector to clean up unreferenced objects but, we must not rely on it to close the file. 65

Example 66

Exception Handling If an exception occurs when we are performing some operation with the file, the code exits without closing the file. 67

The try except Block Error handling in Python is done through the use of exceptions that are caught in try blocks and handled in except blocks. If an error is encountered, a try block code execution is stopped and transferred down to the except block. In addition to using an except block after the try block, you can also use the finally block. The code in the finally block will be executed regardless of whether an exception occurs. 68

The try except Block ( By using try... finally block, we are guaranteed that the file is properly closed even if an exception is raised, causing program flow to stop. 69

Writing to File The write() method writes any string to an open file. It is important to note that Python strings can have binary data and not just text. The write() method does not add a newline character ('\n') to the end of the string. Combine with file access mode, the target file can be overwrite or append 70

Example: Insert Data to a New File By using w in access file, the data will be written to a new file (or overwritten) Content in file Texting.txt 71

Example: Append Data to Existing File By using a in access file, the data will be appended to existing file Content in file Texting.txt 72

Reading from a File To read a file in Python, we must open the file in reading mode. There are several ways to read data from a file. Method read( ) readline( ) readlines( ) Description Return specified number of characters from the file. If omitted it will read the entire contents of the file. Return the next line of the file. Read all the lines as a list of strings in the file 73

Read File using readline() method If you want to read a file line by line, as opposed to pulling the content of the entire file at once, then you use the readline() function. 74

Read File using readlines() method When you use readlines() for reading the file or document line by line, it will separate each line and present the file in a readable format. 75

Read File using read() method If you need to extract a string that contains all characters in the file, you can use the read() method: 76

File Pointer Location We can change our current file cursor (position) using the seek() method. Similarly, the tell() method returns our current position (in number of bytes). 77

Parsing CSV Files The csv library provides functionality to both read from and write to CSV files. Designed to work out of the box with Excel-generated CSV files, it is easily adapted to work with a variety of CSV formats. The csv library contains objects and other code to read, write, and process data from and to CSV files. 78

Reading CSV Files with csv Reading from a CSV file is done using the reader object. The CSV file is opened as a text file with Python s built-in open() function, which returns a file object. This is then passed to the reader, which does the heavy lifting. 79

Reading CSV Files into Dictionary with csv Rather than deal with a list of individual String elements, you can read CSV data directly into a dictionary as well. 80

Writing CSV Files with csv You can also write to a CSV file using a writer object and the.write_row() method: 81

Writing CSV File from Dictionary with csv Unlike DictReader, the fieldnames parameter is required when writing a dictionary. It also uses the keys in fieldnames to write out the first row as column names. 82