week 10 File Reading and Plotting Flashcards

1
Q

open function

A

sets up file as file object, retrieves input from file

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

how to open a file

A

file = open(‘data.txt’)
z = file.readlines()

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

helper functions

A

created to help set up program (ex: read file)

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

steps of plotting, step 1

A

import matlab library

import matplotlib.pyplot as plt

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

steps of plotting, step 2

A

pass value into plot function and show

plt.plot(food)
plt.show()

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

steps of plotting, step 3

A

saving plot

plt.savefig(‘plot.png’)

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

shorthand notation

A

short ways to customize plot color and marker

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

customizing ticks

A

changing the ticks on the plot

plt.xticks(ticks, regions, rotation = ‘20’)
ticks = location you want labeled
regions = new label
rotation = tilt label

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

plt.legend()

A

makes legend of lines on graph

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

file.close()

A

closes file, after called no more reads/writes allowed to file

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

file.read()

A

returns file as a string, you can specify max bytes in ()

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

file.readlines()

A

returns file lines as elements of a list

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

EOF

A

end of file

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

file.write()

A

writes string argument to file

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

mode

A

indicates how the file is opened and permissions

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

‘r’

A

open for reading

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

‘w’

A

open for writing, creates file if one doesn’t exist

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

‘a’

A

open for appending, creates file if one doesn’t exist

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

’ +’

A

can be added to letters to specify, update mode (add reading and writing)

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

output buffer

A

output is buffered by the interpreter before being written on hard drive

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

how to buffer output

A

f = open(‘myfile.txt’, ‘w’, buffering = 100)

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

f.flush()

A

clears internal buffer of a file

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

what type should integers be when being written to files?

A

integers should be converted to strings before being written or appended to files

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

os module

A

provides access to operating system

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
portability
ability to access an item easily, from multiple locations
26
how do you reduce portability?
by hardcoding file paths as string literals
27
path separator
'\\', '/' separator between character directories
28
os.path.sep
stores path separator for the current operating system
29
os.path.join("sub", "output.txt")... output?
sub\\output.txt <-- windows sub/output.txt <-- mac
30
os.walk()
walks a directory tree, iterable object in for loop
31
binary data
data stored as sequence of bytes
32
bytes object
used to represent sequence of single-byte values
33
bytes()
creates byte object
34
binary file mode 'b'
character to open file to binary mode
35
header
sequence constitutes to it, describes bitmaps contents of bytes and unpacking into values
36
struct
module used for packing values into sequences of bytes and unpacking into values
37
struct.pack()
packs values (like str or int) into a sequence of bytes
38
byte order/endianness '<'
determines if the most significant or least significant value is first
39
strut.unpack()
unpacks sequence of bytes into new object
40
with statement
can be used to open file, execute statements, and close file
41
context manager
created by with statement, manages use of resources by setting up and tearing down operations
42
what is good practice to open a file?
use with so that it is closed after you're done using it
43
csv file
text-based file that uses commas to separate fields
44
fields
data items
45
csv module
python library that can be used to read and write csv files
46
how do you read csv files?
csv.reader(csvfile, delimeter = ',')
47
writerow()/writerows()
used to write list of strings into a csv file
48
numpy
python package that provides math computation tools
49
array
multidimensional object with ordered set of elements of the same type
50
shape of array
tuple of array's dimensions
51
size of array
number of elements in the array
52
axis
direction along each array dimension
53
np.zeros((1, 3))
returns 1 by 3 array of zeros
54
np.full(shape, value)
fills shape with value
55
np.delete(ndarray, index, axis)
returns new array with row, col deleted
56
axis = 0
rows
57
axis = 1
columns
58
ndarray.sort(axis)
sorts in ascending order along specified axis
59
ndarray.ravel()
returns flattened version of given array
60
ndarray.transpose()
transposes array
61
np.array()
creates array
62
matplotlib
package to create plots
63
pyplot
creates figures
64
import structure for matplotlib
import matplotlib.pyplot as plt
65
plt.plot()
line plot function
66
plt.scatter(x,y)
scatter plot function
67
plt.figure(figsize = #)
creates new figure for plot to appear
68
plt.show()
displays figure
69
plt.savefig(name)
saves figure as name
70
plt.title()
titles plot
71
plt.xlabel()
labels x axis
72
plt.ylabel()
labels y axis
73
plt.text(x, y, s)
adds string 's' to coordinates x,y
74
annotate(s, xy, xytext)
links s to coordinates xy
75
legend([names], loc = 'lower right')
adds legend with names to specified location (loc)
76
plt.grid(axis = 'x')
adds grid lines to specified axis
77
plt.grid(linestyle = '--')
adds grid lines to both axises
78
plt.subplot()
multiple plots in one figure