1 / 4

Creative Computing

Creative Computing. Reading from Files. Opening a file. Create a file object and store it in a variable <name> = open( " < filename> " ) Example: work_hrs = open(" work.txt"). Line-based processing. A file can be a target in a for… in loop

jadon
Download Presentation

Creative Computing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

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()

More Related