{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

ecs10-11-7 - Announcements ECS 10 11/07 Vishal Vishal will...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 11/7/2008 Announcements ECS 10 11/07 Vishal Vishal will be in lab only from 3-4 today. 3Program Program 5 due Wds Nov 12. Next Next midterm Mon Nov 17. Many Eyes Many Eyes Many Eyes File Format It It wants a table of Tab Separated Values Every Every line is a row of the table Columns Columns within a line are separated by tabs Tab Tab in Python is ‘\t’, just like newline is ‘\n’ ‘\ ‘\ Our Our input data is in this format too. It It is known as .tsv .tsv Can Can be read in Excel, Google docs… This is When We Connect to to the Rest of the World 1 11/7/2008 Special For US Counties Wants Wants just county names, eg. ‘Alameda’, not eg. ‘Alameda ‘Alameda ‘Alameda County’. Wants Wants state, which for us is always ‘California ‘California’. Wants Wants column names at the top of each column. Map Map of Hispanic Population Read Read the census file, write out a .tsv .tsv Look Look at input file. Writing Writing Open Open file for writing: outfile = open(‘latinos.tsv’,’w’) write method write method writes strings as lines of file. outfile.write(‘This line gets written’) Only writes Only writes strings. num = 42 outfile.write(str(num)) outfile.write(‘%d’%num) The Value None Sometimes Sometimes you want to make a variable, but not put anything in it. Like Like making a box, putting a label on it, but not not putting anything inside. Not Not the empty list, not the empty string; it does not have a type. Different Different File Read Loop line = None while line != '': line = censusFile.readline() if line != '': words = line.split('\t') # Other line processing Get Census Data County County is first, then total population, 10th number is the total Hispanic population. Need Need to cut the word ‘County’ off of every county county. Remember Remember these are strings. 2 11/7/2008 All But Last Word countyStr = words[0] countyWords = countyStr.split() county = '' for i in range(len(countyWords)-1): county = county+' '+countyWords[i] Convert to Floats Use Use variable names that say what the data is, including type. Label should say what’s in the box! Use Use replace method to eliminate commas. Other Other options? Calculate Calculate percent Hispanic Write Output File Be Be sure to put a ‘\n’ at the end of every line. ‘\ Separate Separate values with ‘\t’ ‘\ Use Use whacky syntax to get two decimal places: county+'\tCalifornia\t%.2f\n'%percent Open in Excel This This is one way to get data into a spreadsheet. From From there you can make charts, use it to calculate other numbers, etc. Upload Upload to Many Eyes Open Open file as text file (right-click, Open with (rightNotepad). Scrape Scrape data with mouse. Paste into box. Be Be sure to do it outside class Web page (not inside frame). You get a lot more controls! 3 ...
View Full Document

  • Winter '07
  • Amenta
  • Computer file formats, Open Open file, County County, tabs Tab Tab, Tab Separated Values, ‘Alameda ‘Alameda County

{[ snackBarMessage ]}