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
Q

portability

A

ability to access an item easily, from multiple locations

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

how do you reduce portability?

A

by hardcoding file paths as string literals

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

path separator

A

’\’, ‘/’ separator between character directories

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

os.path.sep

A

stores path separator for the current operating system

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

os.path.join(“sub”, “output.txt”)… output?

A

sub\output.txt <– windows
sub/output.txt <– mac

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

os.walk()

A

walks a directory tree, iterable object in for loop

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

binary data

A

data stored as sequence of bytes

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

bytes object

A

used to represent sequence of single-byte values

33
Q

bytes()

A

creates byte object

34
Q

binary file mode ‘b’

A

character to open file to binary mode

35
Q

header

A

sequence constitutes to it, describes bitmaps contents of bytes and unpacking into values

36
Q

struct

A

module used for packing values into sequences of bytes and unpacking into values

37
Q

struct.pack()

A

packs values (like str or int) into a sequence of bytes

38
Q

byte order/endianness ‘<’

A

determines if the most significant or least significant value is first

39
Q

strut.unpack()

A

unpacks sequence of bytes into new object

40
Q

with statement

A

can be used to open file, execute statements, and close file

41
Q

context manager

A

created by with statement, manages use of resources by setting up and tearing down operations

42
Q

what is good practice to open a file?

A

use with so that it is closed after you’re done using it

43
Q

csv file

A

text-based file that uses commas to separate fields

44
Q

fields

A

data items

45
Q

csv module

A

python library that can be used to read and write csv files

46
Q

how do you read csv files?

A

csv.reader(csvfile, delimeter = ‘,’)

47
Q

writerow()/writerows()

A

used to write list of strings into a csv file

48
Q

numpy

A

python package that provides math computation tools

49
Q

array

A

multidimensional object with ordered set of elements of the same type

50
Q

shape of array

A

tuple of array’s dimensions

51
Q

size of array

A

number of elements in the array

52
Q

axis

A

direction along each array dimension

53
Q

np.zeros((1, 3))

A

returns 1 by 3 array of zeros

54
Q

np.full(shape, value)

A

fills shape with value

55
Q

np.delete(ndarray, index, axis)

A

returns new array with row, col deleted

56
Q

axis = 0

A

rows

57
Q

axis = 1

A

columns

58
Q

ndarray.sort(axis)

A

sorts in ascending order along specified axis

59
Q

ndarray.ravel()

A

returns flattened version of given array

60
Q

ndarray.transpose()

A

transposes array

61
Q

np.array()

A

creates array

62
Q

matplotlib

A

package to create plots

63
Q

pyplot

A

creates figures

64
Q

import structure for matplotlib

A

import matplotlib.pyplot as plt

65
Q

plt.plot()

A

line plot function

66
Q

plt.scatter(x,y)

A

scatter plot function

67
Q

plt.figure(figsize = #)

A

creates new figure for plot to appear

68
Q

plt.show()

A

displays figure

69
Q

plt.savefig(name)

A

saves figure as name

70
Q

plt.title()

A

titles plot

71
Q

plt.xlabel()

A

labels x axis

72
Q

plt.ylabel()

A

labels y axis

73
Q

plt.text(x, y, s)

A

adds string ‘s’ to coordinates x,y

74
Q

annotate(s, xy, xytext)

A

links s to coordinates xy

75
Q

legend([names], loc = ‘lower right’)

A

adds legend with names to specified location (loc)

76
Q

plt.grid(axis = ‘x’)

A

adds grid lines to specified axis

77
Q

plt.grid(linestyle = ‘–’)

A

adds grid lines to both axises

78
Q

plt.subplot()

A

multiple plots in one figure