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
``` 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]]
31
parameters for csv.DictWriter
csv.DictWriter(file_object, fieldnames)
32
JSON stands for
javascript object notation
33
JSON uses javascript syntax but it is
text only
34
True and False in python are ___ in javascript
true and false
35
None in python is ___ in javascript
null
36
object in Json is a ___ in python
dict
37
array in JSON is a ___ in python
list
38
string in JSON is a ___ in python
str
39
number (int) in JSON is a ___ in python
int
40
number (real) in JSON is a ___ in python
float
41
true in JSON is a ___ in python
True
42
false in JSON is a ___ in python
False
43
null in JSON is a ___ in python
None
44
Json to python is called
deserialization
45
python to JSON is called
serialization
46
dict in python is ___ in JSON
object
47
list, tuple in python is ___ in JSON
array
48
str in python is ___ in JSON
string
49
int, long, float in python is ___ in JSON
number
50
True in python is ___ in JSON
true
51
False in python is ___ in JSON
false
52
None in python is ___ in JSON
null
53
JSON module method that translates the data in a json file into a python dictionary
.load() | json2dict = json.load(open('jsonfilename.json', 'rt'))
54
JSON module method that translates a string of JSON code, turns it into a python dictionary
.loads() | jsonstring2dict = json.loads('jostling')
55
JSON module methods that takes a python dictionary and "dumps" it into a Json file.
.dump() | json.dump(the dictionary.open('jsonfile.json', 'wt')).
56
what does .dump() return?
nothing. Instead it creates a json file in your current directory.
57
JSON module methods that takes a python dictionary and turns it into a single string
.dumps() | dict2jsonstring = json.dumps(the dictionary)
58
what is XML?
extensible markup language
59
xml is a way to
store and transport data
60
what is a closing tag
>
61
a root is the
first unique tag
62
what does an attribute tag look like with the tag name = section and attributes name and location
63
what is the required import for XML
import xml.etree.ElementTree as ET
64
how do you create an ElementTree object
tree = ET.parse('filename.xml')
65
find the root of an xml
root = tree.getroot()
66
get the list of tagname elements
taglist = treefindall("tagname")
67
finding the first instance of a tagname /subtag within tagname?
tag.find()
68
finding the text between tags?
tag.find().text
69
create an element:
root = ET.Element("rootname")
70
create a child element:
child = ET.Element("child name")
71
append a child element to the root
(root.append(child))
72
how to add attributes to your elements
root.attrib['attribute name'] = str(attribute value)
73
add text to your elements
root.text = "text"
74
HTML stands for
hypertext markup language
75
difference between HTML and XML
XML is for data exchange/storage and HTML is mostly for Data display