Computer Science 151
An introduction to the art of computing
Dictionary Writer
Rudy Martinez
CS151 Spring 2019
File Output
Normal file output:
import csv
writefile = open(‘filename’,’w’,newline=’’) #newline needed if on windows
writer = csv.writer(writefile)
# DATA holds all the records from a dictionary or list
for row in DATA:
writer.writerow(DATA[row]) # this works for both Dictionary but not list
why?
# or
writer.writerows(DATA) # for a dictionary this doesn’t work!!!!
CS151 Spring 2019
Peculiarities of writing lists and dictionaries
List
for row in DATALIST:
writer.writerow(row)
Dictionary
for key in DATADICT.keys():
writer.writerow([key,DATADICT[key]
])
● Remember our last class what was said about for row in DATALIST:
○ For a list a whole row is returned
● Dictionaries are access differently because they are Key:Value pairs.
○ We must create a list of keys to iterate over DATADICT.keys() ← returns list of keys
○ Then we call the DATADICT[key] and we get the Value of that key
● For lists this works well we get a well formed written file.
● For dictionaries we get a key and whatever the Value pair is returned as.
○ In homework 2b we have a list [TMax, TMin]
○ File looks like this: 1994, [77, 47]
CS151 Spring 2019
csv.DictWriter
● DictWriter is a function of the CSV library that knows how to handle Dictionary
objects and can easily write them.
● There is a bit of a catch however …
import csv
writefile = open(‘filename’,’w’,newline=’’) #newline needed if on windows
writer = csv.DictWriter(writefile, fieldnames=[‘date’,’Tmax’,’Tmin’)
writer.writeheader() # Creates header of fieldnames
for key in DATADICT.keys():
# Pattern is key:value
writer.writerow({DATADICT[key][0), DATADICT[key][1),DATADICT[key][2)})
Output:
1994 77.15 47.6
CS151 Spring 2019
What’s the difference
for key in DATADICT.keys():
writer.writerow([key,DATADICT[key]])
Output:
1994, [77.5, 47.6]
Date as string, List of ints.
for key in DATADICT.keys():
# Pattern is key:value
writer.writerow({DATADICT[key][0),
DATADICT[key][1),
DATADICT[key][2)})
Output:
1994, 77.5, 47.6
All values as a csv of strings.
CS151 Spring 2019
Dictionary Representation
for key in DATADICT.keys():
# Pattern is key:value
writer.writerow({DATADICT[key][0), DATADICT[key][1), DATADICT[key][2)})
DATADICT.Keys() Index 0 Index 1 Index 2
Row 0 1994 1994 77.15 47.6
CS151 Spring 2019
Dictionary Representation
for key in DATADICT.keys():
# Pattern is key:value
writer.writerow({DATADICT[key][0), DATADICT[key][1), DATADICT[key][2)})
DATADICT.Keys() Index 0 Index 1 Index 2
Row 0 1994 1994 77.15 47.6
CS151 Spring 2019
Dictionary Representation
for key in DATADICT.keys():
# Pattern is key:value
writer.writerow({DATADICT[key][0), DATADICT[key][1), DATADICT[key][2)})
DATADICT.Keys() Index 0 Index 1 Index 2
Row 0 1994 1994 77.15 47.6
Row 1 1994 1994 74.76 42.15
CS151 Spring 2019
Dictionary Representation
for key in DATADICT.keys():
# Pattern is key:value
writer.writerow({DATADICT[key][0), DATADICT[key][1), DATADICT[key][2)})
DATADICT.Keys() Index 0 Index 1 Index 2
Row 0 1994 1994 77.15 47.6
Row 1 1995 1995 74.76 42.15
Row 2 1996 1996 73.38 44.25
CS151 Spring 2019
Program 2b Rubric 50pts possible
1. Create a file (homework2b.py). 5
2. Your program should open the homework2.csv data file. 5
3. Read the file into a list. You should convert the date to a datetime object,
and you should convert the TMIN and TMAX to integers or float number types.
10
4. You will create a yearly average for TMIN and TMAX. (see pdf for format) 20
5. Create a Dictionary set the key to be the year and the value to be a list containing TMAX, TMIN. 5
6. Write a comma separated value (CSV) file to disk (averagedata.csv) 5
CS151 Spring 2019
Part C Requirements
● Highest temp for each year
○ For each year keep track of the highest temp (max tmax)
● Lowest temp for each year
○ For each year keep track of the lowest temp (min tmin)
● The number of days above average TMAX
○ For each year, if tmax current > avg tmax increment a counter
● The number of day below average TMIN
○ For each year, if tmin current < avg tmin increment a counter
● Write to a csv file,
○ Year, avgTMAX, avgTMIN, High, Low, Days above TMAX, Days below TMIN
○ Write the above file and call it statistics.csv

More Related Content

PDF
Redis Day TLV 2018 - RediSearch Aggregations
PDF
Redis Day TLV 2018 - Redis as a Time-Series DB
PDF
Matlab data import
PPTX
CS 151 CSV output
PPTX
CS 151 homework2a
PDF
rmarkdown.pdf
PDF
RStudio_s_R_Markdown_documentation_Cheat_Cheet__1677232908.pdf
PPTX
CS 151 Date time lecture
Redis Day TLV 2018 - RediSearch Aggregations
Redis Day TLV 2018 - Redis as a Time-Series DB
Matlab data import
CS 151 CSV output
CS 151 homework2a
rmarkdown.pdf
RStudio_s_R_Markdown_documentation_Cheat_Cheet__1677232908.pdf
CS 151 Date time lecture

Similar to Cs 151 dictionary writer (20)

PDF
Class 12 computer sample paper with answers
PPT
ABAP Programming Overview
PPT
chapter-1abapprogrammingoverview-091205081953-phpapp01
PPT
Chapter 1 Abap Programming Overview
PPT
Abapprogrammingoverview 090715081305-phpapp02
PPT
Abapprogrammingoverview 090715081305-phpapp02
PPT
Chapter 1abapprogrammingoverview-091205081953-phpapp01
PDF
XII - 2022-23 - IP - RAIPUR (CBSE FINAL EXAM).pdf
PDF
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21
PDF
Basics of R programming for analytics [Autosaved] (1).pdf
PDF
Chapter 16-spreadsheet1 questions and answer
PPTX
CS 151 Graphing lecture
PDF
Python training for beginners
PPT
Devry cis-170-c-i lab-7-of-7-sequential-files
PPT
Devry cis-170-c-i lab-7-of-7-sequential-files
PPTX
New Features of SQL Server 2016
PPTX
CS151 Deep copy
PDF
C programming & data structure [arrays & pointers]
PPT
Les09 (using ddl statements to create and manage tables)
Class 12 computer sample paper with answers
ABAP Programming Overview
chapter-1abapprogrammingoverview-091205081953-phpapp01
Chapter 1 Abap Programming Overview
Abapprogrammingoverview 090715081305-phpapp02
Abapprogrammingoverview 090715081305-phpapp02
Chapter 1abapprogrammingoverview-091205081953-phpapp01
XII - 2022-23 - IP - RAIPUR (CBSE FINAL EXAM).pdf
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21
Basics of R programming for analytics [Autosaved] (1).pdf
Chapter 16-spreadsheet1 questions and answer
CS 151 Graphing lecture
Python training for beginners
Devry cis-170-c-i lab-7-of-7-sequential-files
Devry cis-170-c-i lab-7-of-7-sequential-files
New Features of SQL Server 2016
CS151 Deep copy
C programming & data structure [arrays & pointers]
Les09 (using ddl statements to create and manage tables)
Ad

More from Rudy Martinez (13)

PPTX
CS 151Exploration of python
PPTX
CS 151 Classes lecture 2
PPTX
CS 151 Classes lecture
PPTX
CS 151 Standard deviation lecture
PPTX
CS 151 Midterm review
PPTX
CS 151 dictionary objects
PPTX
CS151 FIle Input and Output
PPTX
CS151 Functions lecture
PPTX
Lecture4
PPTX
Lecture01
PPTX
Lecture02
PPTX
Lecture03
PPTX
Lecture01
CS 151Exploration of python
CS 151 Classes lecture 2
CS 151 Classes lecture
CS 151 Standard deviation lecture
CS 151 Midterm review
CS 151 dictionary objects
CS151 FIle Input and Output
CS151 Functions lecture
Lecture4
Lecture01
Lecture02
Lecture03
Lecture01
Ad

Recently uploaded (20)

PDF
HVAC Specification 2024 according to central public works department
PDF
LIFE & LIVING TRILOGY- PART (1) WHO ARE WE.pdf
PDF
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 1).pdf
PPTX
Virtual and Augmented Reality in Current Scenario
PDF
Journal of Dental Science - UDMY (2021).pdf
PPTX
Share_Module_2_Power_conflict_and_negotiation.pptx
PDF
FORM 1 BIOLOGY MIND MAPS and their schemes
PDF
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
PDF
English Textual Question & Ans (12th Class).pdf
PDF
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
PDF
Skin Care and Cosmetic Ingredients Dictionary ( PDFDrive ).pdf
PPTX
Unit 4 Computer Architecture Multicore Processor.pptx
PPTX
A powerpoint presentation on the Revised K-10 Science Shaping Paper
PDF
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
PDF
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
PPTX
What’s under the hood: Parsing standardized learning content for AI
PDF
What if we spent less time fighting change, and more time building what’s rig...
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
PDF
Race Reva University – Shaping Future Leaders in Artificial Intelligence
HVAC Specification 2024 according to central public works department
LIFE & LIVING TRILOGY- PART (1) WHO ARE WE.pdf
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 1).pdf
Virtual and Augmented Reality in Current Scenario
Journal of Dental Science - UDMY (2021).pdf
Share_Module_2_Power_conflict_and_negotiation.pptx
FORM 1 BIOLOGY MIND MAPS and their schemes
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
English Textual Question & Ans (12th Class).pdf
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
Skin Care and Cosmetic Ingredients Dictionary ( PDFDrive ).pdf
Unit 4 Computer Architecture Multicore Processor.pptx
A powerpoint presentation on the Revised K-10 Science Shaping Paper
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
What’s under the hood: Parsing standardized learning content for AI
What if we spent less time fighting change, and more time building what’s rig...
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
Race Reva University – Shaping Future Leaders in Artificial Intelligence

Cs 151 dictionary writer

  • 1. Computer Science 151 An introduction to the art of computing Dictionary Writer Rudy Martinez
  • 2. CS151 Spring 2019 File Output Normal file output: import csv writefile = open(‘filename’,’w’,newline=’’) #newline needed if on windows writer = csv.writer(writefile) # DATA holds all the records from a dictionary or list for row in DATA: writer.writerow(DATA[row]) # this works for both Dictionary but not list why? # or writer.writerows(DATA) # for a dictionary this doesn’t work!!!!
  • 3. CS151 Spring 2019 Peculiarities of writing lists and dictionaries List for row in DATALIST: writer.writerow(row) Dictionary for key in DATADICT.keys(): writer.writerow([key,DATADICT[key] ]) ● Remember our last class what was said about for row in DATALIST: ○ For a list a whole row is returned ● Dictionaries are access differently because they are Key:Value pairs. ○ We must create a list of keys to iterate over DATADICT.keys() ← returns list of keys ○ Then we call the DATADICT[key] and we get the Value of that key ● For lists this works well we get a well formed written file. ● For dictionaries we get a key and whatever the Value pair is returned as. ○ In homework 2b we have a list [TMax, TMin] ○ File looks like this: 1994, [77, 47]
  • 4. CS151 Spring 2019 csv.DictWriter ● DictWriter is a function of the CSV library that knows how to handle Dictionary objects and can easily write them. ● There is a bit of a catch however … import csv writefile = open(‘filename’,’w’,newline=’’) #newline needed if on windows writer = csv.DictWriter(writefile, fieldnames=[‘date’,’Tmax’,’Tmin’) writer.writeheader() # Creates header of fieldnames for key in DATADICT.keys(): # Pattern is key:value writer.writerow({DATADICT[key][0), DATADICT[key][1),DATADICT[key][2)}) Output: 1994 77.15 47.6
  • 5. CS151 Spring 2019 What’s the difference for key in DATADICT.keys(): writer.writerow([key,DATADICT[key]]) Output: 1994, [77.5, 47.6] Date as string, List of ints. for key in DATADICT.keys(): # Pattern is key:value writer.writerow({DATADICT[key][0), DATADICT[key][1), DATADICT[key][2)}) Output: 1994, 77.5, 47.6 All values as a csv of strings.
  • 6. CS151 Spring 2019 Dictionary Representation for key in DATADICT.keys(): # Pattern is key:value writer.writerow({DATADICT[key][0), DATADICT[key][1), DATADICT[key][2)}) DATADICT.Keys() Index 0 Index 1 Index 2 Row 0 1994 1994 77.15 47.6
  • 7. CS151 Spring 2019 Dictionary Representation for key in DATADICT.keys(): # Pattern is key:value writer.writerow({DATADICT[key][0), DATADICT[key][1), DATADICT[key][2)}) DATADICT.Keys() Index 0 Index 1 Index 2 Row 0 1994 1994 77.15 47.6
  • 8. CS151 Spring 2019 Dictionary Representation for key in DATADICT.keys(): # Pattern is key:value writer.writerow({DATADICT[key][0), DATADICT[key][1), DATADICT[key][2)}) DATADICT.Keys() Index 0 Index 1 Index 2 Row 0 1994 1994 77.15 47.6 Row 1 1994 1994 74.76 42.15
  • 9. CS151 Spring 2019 Dictionary Representation for key in DATADICT.keys(): # Pattern is key:value writer.writerow({DATADICT[key][0), DATADICT[key][1), DATADICT[key][2)}) DATADICT.Keys() Index 0 Index 1 Index 2 Row 0 1994 1994 77.15 47.6 Row 1 1995 1995 74.76 42.15 Row 2 1996 1996 73.38 44.25
  • 10. CS151 Spring 2019 Program 2b Rubric 50pts possible 1. Create a file (homework2b.py). 5 2. Your program should open the homework2.csv data file. 5 3. Read the file into a list. You should convert the date to a datetime object, and you should convert the TMIN and TMAX to integers or float number types. 10 4. You will create a yearly average for TMIN and TMAX. (see pdf for format) 20 5. Create a Dictionary set the key to be the year and the value to be a list containing TMAX, TMIN. 5 6. Write a comma separated value (CSV) file to disk (averagedata.csv) 5
  • 11. CS151 Spring 2019 Part C Requirements ● Highest temp for each year ○ For each year keep track of the highest temp (max tmax) ● Lowest temp for each year ○ For each year keep track of the lowest temp (min tmin) ● The number of days above average TMAX ○ For each year, if tmax current > avg tmax increment a counter ● The number of day below average TMIN ○ For each year, if tmin current < avg tmin increment a counter ● Write to a csv file, ○ Year, avgTMAX, avgTMIN, High, Low, Days above TMAX, Days below TMIN ○ Write the above file and call it statistics.csv