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

L8 Strings Files SP20 - University of Illinois Urbana

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Lecture 8: Strings and Files

Craig Zilles (Computer Science)

March 22, 2020

https://go.illinois.edu/cs105sp20

CS 105

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

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

2

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

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

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

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

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

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

Format string variable formatting

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

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

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

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

9

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

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

Write programs a few lines at a time!

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

12

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

Diary that records user input into a file.

14

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

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

Create a list of Illinois U.S. representatives

17

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

18

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

Filtering a collection (pattern)

newlist = []

for thing in collection:if thing meets criteria:

newlist.append(thing)

20