Assessment 3 Flashcards

1
Q

open a file

A

my_file = open(‘filename’, ‘access mode’)

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

close a file

A

my_file.close()

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

access modes for reading

A

r or rt

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

access modes for writing

A

wt or w

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

access modes for appending

A

at or a

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

reading/writing in binary format

A

rb or wb

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

return the whole file in one big string

A

file_handle.read()

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

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

A

file_handle.readline()

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

where does file_handle.readline() set the cursor to

A

the next line

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

move the cursor manually

A

file_handle.seek()

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

how to loop over a file

A

for line in file_handle:

print(line)

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

writes a single string into the file

A

.write()

file_handle.write(‘dfdfdf\n’)

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

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

A

.writelines()

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

using a content manager:

A

with open(“filename”, “access mode”) as file_handle:

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

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

A

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

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

what does buffered mean

A

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

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

what do you put under the try block

A

you write the code that you expect may throw an error

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

what do you put under the except block

A

code to execute if an error occurs

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

what do I import to deal with csv files

A

import csv

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

parameters for csv.reader

A

csv.reader(file_object, delimiter = ‘,’)

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

what are the parameters for csv.DictReader

A

csv.DictReader(file_object,fieldnames)

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

if the filename parameter for csv.DictReader is not specified

A

the keys default to the first row

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

what are the parameters for csv.writer

A

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

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

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q
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]
A

list

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

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

A

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

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

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

A

quotechar of a CSV file

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

default value for the quotechar is :

A

double quote (“)

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

quotechar can be a parameter in

A

csv.reader()

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

convert a listed list into a dictionary in one line

A

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

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

parameters for csv.DictWriter

A

csv.DictWriter(file_object, fieldnames)

32
Q

JSON stands for

A

javascript object notation

33
Q

JSON uses javascript syntax but it is

A

text only

34
Q

True and False in python are ___ in javascript

A

true and false

35
Q

None in python is ___ in javascript

A

null

36
Q

object in Json is a ___ in python

A

dict

37
Q

array in JSON is a ___ in python

A

list

38
Q

string in JSON is a ___ in python

A

str

39
Q

number (int) in JSON is a ___ in python

A

int

40
Q

number (real) in JSON is a ___ in python

A

float

41
Q

true in JSON is a ___ in python

A

True

42
Q

false in JSON is a ___ in python

A

False

43
Q

null in JSON is a ___ in python

A

None

44
Q

Json to python is called

A

deserialization

45
Q

python to JSON is called

A

serialization

46
Q

dict in python is ___ in JSON

A

object

47
Q

list, tuple in python is ___ in JSON

A

array

48
Q

str in python is ___ in JSON

A

string

49
Q

int, long, float in python is ___ in JSON

A

number

50
Q

True in python is ___ in JSON

A

true

51
Q

False in python is ___ in JSON

A

false

52
Q

None in python is ___ in JSON

A

null

53
Q

JSON module method that translates the data in a json file into a python dictionary

A

.load()

json2dict = json.load(open(‘jsonfilename.json’, ‘rt’))

54
Q

JSON module method that translates a string of JSON code, turns it into a python dictionary

A

.loads()

jsonstring2dict = json.loads(‘jostling’)

55
Q

JSON module methods that takes a python dictionary and “dumps” it into a Json file.

A

.dump()

json.dump(the dictionary.open(‘jsonfile.json’, ‘wt’)).

56
Q

what does .dump() return?

A

nothing. Instead it creates a json file in your current directory.

57
Q

JSON module methods that takes a python dictionary and turns it into a single string

A

.dumps()

dict2jsonstring = json.dumps(the dictionary)

58
Q

what is XML?

A

extensible markup language

59
Q

xml is a way to

A

store and transport data

60
Q

what is a closing tag

A

>

61
Q

a root is the

A

first unique tag

62
Q

what does an attribute tag look like with the tag name = section and attributes name and location

A
63
Q

what is the required import for XML

A

import xml.etree.ElementTree as ET

64
Q

how do you create an ElementTree object

A

tree = ET.parse(‘filename.xml’)

65
Q

find the root of an xml

A

root = tree.getroot()

66
Q

get the list of tagname elements

A

taglist = treefindall(“tagname”)

67
Q

finding the first instance of a tagname /subtag within tagname?

A

tag.find()

68
Q

finding the text between tags?

A

tag.find().text

69
Q

create an element:

A

root = ET.Element(“rootname”)

70
Q

create a child element:

A

child = ET.Element(“child name”)

71
Q

append a child element to the root

A

(root.append(child))

72
Q

how to add attributes to your elements

A

root.attrib[‘attribute name’] = str(attribute value)

73
Q

add text to your elements

A

root.text = “text”

74
Q

HTML stands for

A

hypertext markup language

75
Q

difference between HTML and XML

A

XML is for data exchange/storage and HTML is mostly for Data display