Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSV files Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction.

Similar presentations


Presentation on theme: "CSV files Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction."— Presentation transcript:

1 CSV files Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction to Computer Science, 2nd edition, by John Zelle and copyright notes by Prof. George Heineman of Worcester Polytechnic Institute) CS-1004, A-Term 2014 CSV files

2 Introduction to CSV files
“CSV” — Comma Separated Values Common format for interchange among spreadsheets One “Header” row Many “rows” of data Each row is called a “record” Record:– String containing “fields” separated by commas Each field is a substring of text “Meanings” of fields Defined by Header Row CS-1004, A-Term 2014 CSV files

3 Example Header Row:– Data (subsequent rows)
LastName,FirstName,Initial,Course,Time Data (subsequent rows) Lauer,Hugh,C,CS-1004,0800 Wills,Craig,E,CS-3013,1200 Hamel,Glynis,M,CS-1101,1000 Rich,Charles,,CS-1102,1000 Easily import to / exported from spreadsheet Each “row” of csv-file is row of spreadsheet Commas delineate columns Header row indicates meanings of columns CS-1004, A-Term 2014 CSV files

4 Processing CSV files in Python
Module “csv” Methods csv.reader() — returns a “reader” object csv.writer() — returns a “writer” object csv.DictReader() — returns a “reader” object that makes each row a mini dictionary csv.DictWriter() — returns a “writer” object that constructs output from mini dictionary Other methods Limit field sizes Manage “dialects” of csv files “Sniff” out the format of a csv file CS-1004, A-Term 2014 CSV files

5 CSV Reader Acts somewhat like file.read()
for row in reader: #process row list.append(row) Format of each row:– List of strings Each element of list represents one field Empty fields from csv file  null strings in list Numbers not by default interpreted! Settable option Example smallDataSet.csv CS-1004, A-Term 2014 CSV files

6 CSV DictReader Each row is actually a mini-dictionary
Key names taken from header row By default! Controllable Key names of all rows are same Enables access to fields by name Without having to count fields in rows Combining multiple data sets with different formats E.g., two data sets may both have 'latitude' and 'longitude' fields but differ in all other respects CS-1004, A-Term 2014 CSV files

7 Questions? CS-1004, A-Term 2014 CSV files

8 Homework #6 Plotting civic data on a map Two data sets
Crime records in Sacramento, CA for January 2006 Real estate transactions in Sacramento region for one week in May 2008 Both as CSV files Each record includes latitude and longitude Draw colored circles on map of region Using graphics package from textbook graphics.Image() object CS-1004, A-Term 2014 CSV files

9 Apologia Homework assignment not yet on course web site
As soon as possible Due Monday, October 13 CS-1004, A-Term 2014 CSV files

10 Questions? CS-1004, A-Term 2014 CSV files


Download ppt "CSV files Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction."

Similar presentations


Ads by Google