2. CS151 Spring 2019
Notes on Homework 2a
1. Open file and pass to file object (myfile, open(file,r))
2. Call csv reader on file object and pass to variable (myfile)
3. Create global dictionary to hold values (DATA = {})
a. Key = date,
b. Value = [tmax, tmin]
4. Create counter for the number of records.
5. For every line in myfile variable
a. If counter == 0 then eat header
i. Increment counter
b. Else
i. If tmax is not empty AND tmin is not empty
1. Then write row to global dictionary (DATA)
2. Increment counter
ii. Else:
1. Increment counter (drop row from final list)
6. Open file and pass to file object (myNewFile, open(newfile, w))
7. For every row in dictionary {DATA) (for loop)
a. Write row to myNewFile object (csv.writerow)
3. CS151 Spring 2019
Writing Dictionary or List to CSV
This is just like writing a normal file.
#Create file object!
myNewFile = open(‘test1.csv’, ‘w’) # This will overwrite test1 each time it runs!
#Create variable to write to
writer = csv.writer(myNewFile)
#write each row in Data List/Dictionary to file
for row in DATA:
writer.writerow(row) # write a single row (as retrieved by for loop)
Output:
['1994-04-01', '74', '35']
['1994-04-02', '68', '38']
['1994-04-04', '70', '']
4. CS151 Spring 2019
Writing Dictionary or List to CSV
This is an alternate way of writing to a csv file.
#Create file object!
myNewFile = open(‘test2.csv’, ‘w’) # This will overwrite test1 each time it runs!
#Create variable to write to
writer = csv.writer(myNewFile)
#write each row in Data List/Dictionary to file
writer.writerows(DATA) # write all rows at once!
Output:
['1994-04-01', '74', '35']
['1994-04-02', '68', '38']
['1994-04-04', '70', '']
5. CS151 Spring 2019
Paths on Linux and Windows
● Paths are basically the same for Linux and Mac!
● Linux =(‘/home/[username]/Documents/myfile.txt’)
● Windows =(r ‘C:Users[username]Documentsmyfile.txt’)
○ Must add ‘r’ to the command for it to read the backslash
● Linux doesn’t need to be told what drive (C: on windows)
● Windows uses a backslash (‘’) and Linux/Mac use a forward slash (‘/’)
Notes for Linux/Mac: if you see a tilde (‘~’) that usually means home folder:
‘~/Documents’ == ‘/home/bob/Documents’
6. CS151 Spring 2019
Summation Exercise
DATA = [‘1’,’1’,’1’,’1’]
summation = 0
for row in DATA:
summation += int(row[0])
print(str(sumation))
Output:
4
* A note your CSV files are all strings!
7. CS151 Spring 2019
Summation Example 2
# New list with 5 elements of type int
DATA2 = [1,1,1,1,1]
# Initialize sum variable to 0
summation1 = 0
# can use sum function from python!
summation1 = sum(DATA2)
# Print result
print(summation1)
Output:
5
8. CS151 Spring 2019
There is a difference!
DATA = [‘1’,’1’,’1’,’1’]
DATA2 = [1,1,1,1,1]
These are two completely different lists
DATA == strings
DATA2 == ints
!Sum only works on list of ints, or floats!
9. CS151 Spring 2019
Conversions
● Convert from string to int
○ int(‘1’) -> 1
● Convert from int to string
○ str(1) -> ‘1’
● Convert from string to datetime object!
from datetime import datetime
from datetime import date
myString = ‘1994-04-01’
myDateVar2 = datetime.strptime(myString, '%Y-%m-%d').date()
myString myDateVar2.year myDateVar2.month
myDateVar2.day
‘1994-04-01’ 1994 04
01
10. CS151 Spring 2019
Lets convert some lists
DATA3 = ['1','2','34','56']
# Now lets convert in place!
for counter in range(0, len(DATA3), 1):
# Grab string and convert to integer and store in variable
myInt = int(DATA3[counter])
# Copy back to list (remember it's mutable so we can do this)
DATA3[counter] = myInt
# Now I can just call sum on DATA3 since it's all int
print('Summation of whole list = ', str(sum(DATA3)))
print('Average of list items is = ', str(sum(DATA3)/len(DATA3)))
Output:
Summation of whole list = 93
Average of list items is = 23.25
11. CS151 Spring 2019
Let’s Review
● Write out what you need to do for the homework using the algorithm given.
○ Think about how to eat the elephant.