ecs10-11-09

ecs10-11-09 - 11/9/2007 Announcements ECS 10 11/09 I 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/9/2007 Announcements ECS 10 11/09 I will be in lab hours this afternoon. Program 5 due Wds Nov ?? Next midterm Mon Nov ??. Wds Nov ?? will be more review and examples; no new material. Billboard Top Ten Read the text file, write it out as .csv .csv Lines look like this: 1 "Chris Brown Featuring T-Pain" "Kiss TKiss" Kiss Ki " 7 Number, artist, song, weeks on chart. Not separated by commas First, read the right lines Writing Open file for writing: outfile = open("billboard.csv", "w") write method writes strings as lines of file. outfile.write("This line gets written") Splitting long strings Remember the split command: str = "Alamo,San Antonio,Texas,Free" fields = str.split(",") fields is now a list of strings strings. Here we have no commas. MultiMulti-word fields in quotes How to separate? 1 11/9/2007 Splitting on " " Often it is useful to split on spaces. quote = "These are times that try mens souls" words = quote.split(" ") Now words contains a list of the words in the sentence. sentence Probably your best choice in the project. Splitting on " We get more or less the fields we want. Field 2 is all spaces, we can throw it away. Need to "clean up" some of the rest.... Trimming fields Need to cut out parts from strings Cutting out a chunk of string string[2:7] This Is called string slicing in Python. Can do this on lists too, if necessary. Negative indices Can access list elements or string characters with negative indices. s = "Armadillo" Now s[-1] i " " and s[-3] is "i" N s[- is "o", d s[- i "i". [ [ Trimming just the back or front Cutting off the last character: string[:string[:-1] Cutting off the first character string[1:] Tidy up fields Cut space off of end of number string. Cut space off beginning and newline off end of weeks. weeks. number = number[:-1] weeks = weeks[1:-1] 2 11/9/2007 Write output Replacing parts of strings Common problem with number strings (you might encounter in assignment) - there's commas inside them. We could write a function to remove the commas. You should all know how to do this. Or, we could see if there is an appropriate string method.... The replace method Google "Python string methods" Get rid of T-Pain T- s = "Flinch" s = s.replace("Fl","Gr") 3 ...
View Full Document

This note was uploaded on 04/14/2008 for the course ECS 10 taught by Professor Amenta during the Fall '07 term at UC Davis.

Ask a homework question - tutors are online