Assessment 3 Flashcards Preview

CS 2316 > Assessment 3 > Flashcards

Flashcards in Assessment 3 Deck (75)
Loading flashcards...
1

open a file

my_file = open('filename', 'access mode')

2

close a file

my_file.close()

3

access modes for reading

r or rt

4

access modes for writing

wt or w

5

access modes for appending

at or a

6

reading/writing in binary format

rb or wb

7

return the whole file in one big string

file_handle.read()

8

returns a single line of the file in a string each time

file_handle.readline()

9

where does file_handle.readline() set the cursor to

the next line

10

move the cursor manually

file_handle.seek()

11

how to loop over a file

for line in file_handle:
print(line)

12

writes a single string into the file

.write()
file_handle.write('dfdfdf\n')

13

writes a list of strings into the file ~ doesn't add new line characters (or anything else) in between

.writelines()

14

using a content manager:

with open("filename", "access mode") as file_handle:

15

when using a content manager, do I need to close it

no, once leaving the indentation from the header, the file is automatically closed by python

16

what does buffered mean

files aren't updated until you close the file object in python

17

what do you put under the try block

you write the code that you expect may throw an error

18

what do you put under the except block

code to execute if an error occurs

19

what do I import to deal with csv files

import csv

20

parameters for csv.reader

csv.reader(file_object, delimiter = ',')

21

what are the parameters for csv.DictReader

csv.DictReader(file_object,fieldnames)

22

if the filename parameter for csv.DictReader is not specified

the keys default to the first row

23

what are the parameters for csv.writer

csv.writer(file_object, delimiter = ',', lineterminator = '\n')

24

how should the filenames parameter be passed in for csv.DictReader

reader = csv.DictReader(my_file, fieldnames = ['word', 'num'])

25

what is the data type of output
with open("example.csv") as my_file:
reader = csv.DictReader(my_file)
output = [ line for line in reader]

list

26

example of using sorted to sort a list of lists by the third index

sorted(aList, key = lambda x:x[2], reverse = True)

27

string that groups elements together and tells python to ignore any time the delimiter appears inside

quotechar of a CSV file

28

default value for the quotechar is :

double quote (")

29

quotechar can be a parameter in

csv.reader()

30

convert a listed list into a dictionary in one line

other_data = [{data[0][0]: i, data[0][1]:j} for i,j in data[1:J]]