-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSC107Lab9bak
74 lines (54 loc) · 3.1 KB
/
CSC107Lab9bak
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Blair Hagen
# 3-8-2016
# CSC 107 Lab 9
# As a student at Union College, I am part of a community that values intellectual effort, curiosity and discovery. I understand that in order to truly claim my educational and academic achievements, I am obligated to act with academic integrity. Therefore, I affirm that I will carry out my academic endeavors with full academic honesty, and I rely on my fellow students to do the same.
# Assignment: Do some stuff with text
import urllib
# Opens and copies the text from "footy.txt," makes it all lowercase, then counts the number of times "football" appears in it.
def footy():
filepath = getMediaPath('footy.txt.')
myFile = open(filepath, 'r')
text = myFile.read()
myFile.close()
# Make all the text lowercase so we don't have to search for 'Football' as well
textLower = text.lower()
count = textLower.count('football')
print 'The word football appears ', count , ' times'
# Takes a sequence, finds the first occurance of sequence, prints the index number of occurance and the name of the parasite containing the sequence.
def findSequence(sequence):
filepath = getMediaPath('parasites.txt')
myFile = open(filepath, 'r')
text = myFile.read()
myFile.close()
foundIndex = text.find(sequence)
if foundIndex == -1:
print('SUBSEQUENCE not found')
else:
print 'The first index # of that sequence is: ', foundIndex
endOfName = text.rfind('\n', 0, foundIndex)
startOfName = text.rfind('>', 0, foundIndex)
print 'The Name of the parasite containing that sequence is:', text[startOfName + 1:endOfName]
# Reads the text from html page, finds the index to start searching at, then finds the two index values to slice between. Prints the temperature.
def atlantaTempLocal():
filepath = getMediaPath('ajc-weather.html')
myFile = open(filepath, 'r')
text = myFile.read()
myFile.close()
beforeTemp = '<font size="+2">'
afterTemp = '<b>°'
todayIndex = text.find('Today in Atlanta')
sizeIncreaseIndex = text.find(beforeTemp, todayIndex)
degreesIndex = text.find(afterTemp, sizeIncreaseIndex)
# Prints the temperature by slicing whatever is after the 'beforeIndex' and ends at the 'afterIndex'
print 'The temperature in Atlanta was:' , text[sizeIncreaseIndex + len(beforeTemp):degreesIndex] , 'degrees'
# Reads from the atlanta weather webpage, finds the two index values to slice between. Prints the current 'feels like' temperature.
def atlantaTempLive():
connection = urllib.urlopen("http://www.ajc.com/weather/30301/")
weatherData = connection.read()
connection.close()
beforeTemp = 'Feels like'
afterTemp = '°'
beforeIndex = weatherData.find(beforeTemp)
afterIndex = weatherData.find(afterTemp, beforeIndex)
# Prints the temperature by slicing whatever is after the 'beforeIndex' and ends at the 'afterIndex'
print 'The temperature in Atlanta currently feels like:' , weatherData[beforeIndex + len(beforeTemp):afterIndex] , 'degrees'