The document discusses using the CSV module in Python to work with CSV files where rows can be accessed as dictionaries by using the DictReader and DictWriter classes. DictReader allows iterating over rows and accessing fields by name, inferring names from the first row. DictWriter requires specifying field names and writes rows from a dictionary, where keys must match field names. Examples are given for reading and writing CSV files using these classes.
2. CSV MODULE (Continued)
Using Field Names
• CSV module includes classes for working with
rows as dictionaries so that the fields can be
named.
• The DictReader and DictWriter classes
translate rows to dictionaries instead of lists.
Keys for the dictionary can be passed in, or
inferred from the first row in the input.
3. DictReader
import csv
f = open('sampleread.csv', 'rt')
try:try:
reader = csv.DictReader(f)
for row in reader:
print row
finally:
f.close()
4. DictWriter
• The main difference is that the fieldnames
have to be entered as a separate list.
• The fieldnames have to match all of the keys• The fieldnames have to match all of the keys
in the dictionary array but can be ordered
differently
6. Advanced Data Structures: Stacks
Data structure to store the elements,
where the main operation is to push,
pop, display.
push – to add an element to the endpush – to add an element to the end
pop – to remove an element from the end
Display – to display the elements
Follows Last In First Out (LIFO) concept
7. Data structure to store the elements, where the
main operation is to enqueue, dequeue,
display.
enqueue – to add an element to the back
Advanced Data Structures: Queues
enqueue – to add an element to the back
dequeue – to remove an element from the front
Display – to display the elements
Follows First In First Out (FIFO) concept
9. TASK
• Write a program to implement Stack
operations
• Write a program to implement Queue
operations
10. TASK
• Given 3 int values, a b c, return their sum.
However, if one of the values is 13 then it does
not count towards the sum and values to its
right do not count. So for example, consider
(a,b,c) if b is 13, then both b and c do not(a,b,c) if b is 13, then both b and c do not
count.
lucky_sum(1, 2, 3) → 6
lucky_sum(1, 2, 13) → 3
lucky_sum(1, 13, 3) → 1
11. TASK
• Given three ints, a b c, one of them is small, one is
medium and one is large. Return true if the three
values are evenly spaced, so the difference between
small and medium is the same as the difference
between medium and large.
• even_space(2,4,6) – True
• even_space(4,5,2) – False
• even_space(4,6,2) – True
(Can make use of any python functions)