Top Banner
Lecture 8: Strings and Files Craig Zilles (Computer Science) March 22, 2020 https://go.illinois.edu/cs105sp20 CS 105
20

L8 Strings Files SP20 - University of Illinois Urbana ...

Oct 24, 2021

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: L8 Strings Files SP20 - University of Illinois Urbana ...

Lecture 8: Strings and Files

Craig Zilles (Computer Science)

March 22, 2020

https://go.illinois.edu/cs105sp20

CS 105

Page 2: L8 Strings Files SP20 - University of Illinois Urbana ...

Today1. Slicing2. Splitting and Joining3. String Formatting (redux)4. Files• Extensions, • Writing, flushing, closing

5. Comma-separated values (CSV) files• Reading, splitting & filter

2

Page 3: L8 Strings Files SP20 - University of Illinois Urbana ...

Slicing• For string slicing the main part that confuses me is

counting spaces• how do negative numbers work when slicing?

my_str = "CS 105"print(my_str[:3])print(my_str[2:])print(my_str[1:4])

3

String 6 001000011 001010011

TypeNumber ofcharacters

Characters(Stored using Unicode encoding)

000100000 000110001 000110000 000110101‘CS 105’

C S 1 0 5

Page 4: L8 Strings Files SP20 - University of Illinois Urbana ...

Slicing

my_str = "CS 105"print(my_str[-4:-2])

A) 'S 1'B) 'S 10'C) ' 1'D) ' 10' E) None of these

4

String 6 001000011 001010011

TypeNumber ofcharacters

Characters(Stored using Unicode encoding)

000100000 000110001 000110000 000110101‘CS 105’

C S 1 0 5

Page 5: L8 Strings Files SP20 - University of Illinois Urbana ...

Slicing out specific regions• Example: Remove all parenthesized regions from a string• "This string (foo) has (bar) things"

def remove_parentheticals(string):while '(' in string:

open_index = string.find('(')close_index = string.find(')')string = string[:open_index] +

string[close_index + 2:]return string

5

Page 6: L8 Strings Files SP20 - University of Illinois Urbana ...

String splitting• I really do not know how the separators will perform and

why; that is, what will be the result of the split() confuses me a lot.• Will the split function always return a list or are there

exceptions?

• String splitting is when you have a string w/separators• Normal sentences using whitespace• "This is a sentence.".split()

• Comma separate variables• "1, 2, 3, 4".split(', ')

6

Page 7: L8 Strings Files SP20 - University of Illinois Urbana ...

String joining• The opposite of splitting

• Pretty common pattern:

mylist = input.split(separator)… process mylist …output = separator.join(mylist)

output = ','.join(input.split(',')[::2])

7

Page 8: L8 Strings Files SP20 - University of Illinois Urbana ...

Python Format Strings• a_string.format(parameters)

• "a {} way".format("better")

• "can {1} the {0}".format("order", "control")

• "precision {0:.2f}".format(math.pi)

8

Page 9: L8 Strings Files SP20 - University of Illinois Urbana ...

Format string variable formatting

• "{0:05.2f}".format(a)) # num digits

• '{:<30}'.format(a) # < > ^

• '{:.^30}'.format(a) # fill with chars

• '{:,}'.format(1234567890) # commas

9

Page 10: L8 Strings Files SP20 - University of Illinois Urbana ...

Files• Files are how data is stored on external storage (disk)• Managed by O/S

• Disk is slow• opening files moves them to memory for the program• Data is buffered (temporarily stored) in memory

10

Page 11: L8 Strings Files SP20 - University of Illinois Urbana ...

Viewing the filesystem• Mac Finder / Windows explorer

• Command line:• Mac: ls ls -l• Windows: dir

• Shows all of the files in the current directory• Can show file size

• Looking at files (Mac): more one page at a timecat whole file at once

11

Page 12: L8 Strings Files SP20 - University of Illinois Urbana ...

Write programs a few lines at a time!

Test every couple lines to make sure they do what you want!

12

Page 13: L8 Strings Files SP20 - University of Illinois Urbana ...

Writing to files• file_object = open('filename', 'w')• file_object.write('thing to write')• file_object.close() automatic at program end • file_object.flush() optional

Example program 1:Diary that records user input into a file.

with open('filename', 'w') as outf:closes file when code block ends

13

Page 14: L8 Strings Files SP20 - University of Illinois Urbana ...

Diary that records user input into a file.

14

Page 15: L8 Strings Files SP20 - University of Illinois Urbana ...

Continue writing to existing file?(i.e., new writes go to end of file)

A) open('filename', 'r')B) open('filename', 'x')C) open('filename', 'i')D) open('filename', 'a')E) open('filename', 'e')

15

Page 16: L8 Strings Files SP20 - University of Illinois Urbana ...

Comma-separated value (CSV) files• Commonly-used data file format• Can be generated from Excel and other spreadsheets

Processing a CSV manually:• Each row is its own line of the file• Can use split(',') to separate the columns• Use indexing to read columns of interest

Example program 2:Create a list of Illinois U.S. representatives

16

Page 17: L8 Strings Files SP20 - University of Illinois Urbana ...

Create a list of Illinois U.S. representatives

17

Page 18: L8 Strings Files SP20 - University of Illinois Urbana ...

Reading from files• file_object = open('filename')• lines = file_object.readlines()• for line in lines:

18

Page 19: L8 Strings Files SP20 - University of Illinois Urbana ...

Skipping first lineA) for line in lines[:1]:B) for line in lines[1:]:C) for line in lines[:2]:D) for line in lines[2]:E) for line in lines[2:]:

19

Page 20: L8 Strings Files SP20 - University of Illinois Urbana ...

Filtering a collection (pattern)

newlist = []

for thing in collection:if thing meets criteria:

newlist.append(thing)

20