Chapter 7 - Text processing Flashcards
title and capitalize both uppercase their respective first letters and…
lowercase everything else
what will this return:
mysentence = “This-should———-be–fun!”
mysentence.split(“-“)
[‘This’, ‘should’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘be’, ‘’, ‘fun!’]
every pair of delimeters = 1 empty space
what will this return: # 21
comma = “fksjefijefk “
print(comma.isalpha())
print(comma.isalnum())
print(comma.isdigit())
False
False
False
spaces arent alphabetical or alphanumeric or digits!
do you need to import string to use the constants?
yes
what will this return:
mystring = “welcome to the ipp”
result = mystring.split()
result
[‘welcome,’ ‘to’, ‘the’, ‘ipp’]
always returns single quotes!
“abc”.rjust(5)
’ abc’
two spaces, single quotes
how would you open a file and read it?
f = open(“filename”, ‘r’)
possible modes in an open function:
‘r’ - reading
‘w’ - writing
‘a’ - appending
‘r+’ = read from and write to file
what happens if you write to a file that already exists?
the contents get completely replaced
open
f = open(“filename”, ‘modekeyletter’)
f = open(“filename”)
second version will open in read mode bcz its the safest
write and writelines
write(s)
write a string s to the file
writelines(ls)
writes a list ls to the file
files that are opened and used must be…
closed, using the close method
f.close()
close
f.close()
closes the file, preventing error
writing vs appending to a file
writing starts from beginning and replaces insides, appending adds to existing parts of file
read, readlines, and readline
f.read()
reads entire file as one string
f.readlines()
reads file into list of strings, each string representing a line
f.readline()
reads one individual line from the file as a string
returns either empty string or list based on what the read returns normally if it is called after the whole file has already been read. Because empty strings are an alt way to say False, you can do a
aline = f.readline()
while aline:
and itll run until readline returns an empty
(does this mean you can call readlines() and if you call read() it will produce a empty string?)