Download
creative computing n.
Skip this Video
Loading SlideShow in 5 Seconds..
Creative Computing PowerPoint Presentation
Download Presentation
Creative Computing

Creative Computing

107 Views Download Presentation
Download Presentation

Creative Computing

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Creative Computing Reading from Files

  2. Opening a file • Create a file object and store it in a variable • <name> = open("<filename>") • Example: work_hrs = open("work.txt")

  3. Line-based processing • A file can be a target in a for… in loop • for <var> in <file>: <var> = <var>.strip() # takes off white space • Example:for line in work_hrs: line = line.strip()

  4. Split a line • Recall: strings can be split • If we have a text file that looks like: • 123 Susan 12.5 8.1 7.6 3.2 • We can split it into the list: • ['123', 'Susan', '12.5', '8.1', '7.6', '3.2‘] for line in work_hrs:line = line.strip() tokens = line.split()