Chapter 7 - Text processing Flashcards

1
Q

title and capitalize both uppercase their respective first letters and…

A

lowercase everything else

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what will this return:
mysentence = “This-should———-be–fun!”
mysentence.split(“-“)

A

[‘This’, ‘should’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘’, ‘be’, ‘’, ‘fun!’]

every pair of delimeters = 1 empty space

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what will this return: # 21
comma = “fksjefijefk “
print(comma.isalpha())
print(comma.isalnum())
print(comma.isdigit())

A

False
False
False

spaces arent alphabetical or alphanumeric or digits!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

do you need to import string to use the constants?

A

yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what will this return:
mystring = “welcome to the ipp”
result = mystring.split()
result

A

[‘welcome,’ ‘to’, ‘the’, ‘ipp’]

always returns single quotes!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

“abc”.rjust(5)

A

’ abc’

two spaces, single quotes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

how would you open a file and read it?

A

f = open(“filename”, ‘r’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

possible modes in an open function:

A

‘r’ - reading
‘w’ - writing
‘a’ - appending
‘r+’ = read from and write to file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what happens if you write to a file that already exists?

A

the contents get completely replaced

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

open

A

f = open(“filename”, ‘modekeyletter’)
f = open(“filename”)

second version will open in read mode bcz its the safest

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

write and writelines

A

write(s)
write a string s to the file

writelines(ls)
writes a list ls to the file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

files that are opened and used must be…

A

closed, using the close method
f.close()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

close

A

f.close()
closes the file, preventing error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

writing vs appending to a file

A

writing starts from beginning and replaces insides, appending adds to existing parts of file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

read, readlines, and readline

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

how to use with statement?

A

with open(filename) as f:
#stuff

closes automatically outside of the code block!

17
Q

whats going on with the /n’s at the end of each line? rstrip method? what? (look over lecture notes)

A
18
Q
A
19
Q

what do read, readlines, and readline return if theyve already read the whole file?

A