Skip to content

Instantly share code, notes, and snippets.

View KarenTheEarth's full-sized avatar

KarenTheEarth

View GitHub Profile
fname = input("Enter file name: ")
if len(fname) < 1 : fname = "mbox-short.txt"
fh = open(fname)
count = 0
for line in fh:
x=line.rstrip().split()
if 'From' in x:
print(x[1])
count+=1
@MichelleDalalJian
MichelleDalalJian / py4e_ex_11
Created October 7, 2017 14:48
Extracting Data With Regular Expressions Finding Numbers in a Haystack In this assignment you will read through and parse a file with text and numbers. You will extract all the numbers in the file and compute the sum of the numbers. Data Files We provide two files for this assignment. One is a sample file where we give you the sum for your testi…
import re
hand = open("regex_sum_24962.txt")
x=list()
for line in hand:
y = re.findall('[0-9]+',line)
x = x+y
sum=0
for z in x:
@MichelleDalalJian
MichelleDalalJian / py4e_ex_09_04
Created October 7, 2017 14:43
9.4 Write a program to read through the mbox-short.txt and figure out who has the sent the greatest number of mail messages. The program looks for 'From ' lines and takes the second word of those lines as the person who sent the mail. The program creates a Python dictionary that maps the sender's mail address to a count of the number of times th…
fname = input("Enter file:")
if len(fname) < 1 : name = "mbox-short.txt"
hand = open(fname)
lst = list()
for line in hand:
if not line.startswith("From:"): continue
line = line.split()
lst.append(line[1])
@zcakzwa
zcakzwa / gist:2bf34f782db0c43847250b5fa86bc2bf
Created July 5, 2016 12:43
10.2 Write a program to read through the mbox-short.txt and figure out the distribution by hour of the day for each of the messages. You can pull the hour out from the 'From ' line by finding the time and then splitting the string a second time using a colon. From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008 Once you have accumulated the c…
name = raw_input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
d=dict()
for line in handle:
if not line.startswith("From "):
continue
else:
line=line.split()
line=line[5]
@tombrad
tombrad / gist:4697060
Last active March 14, 2024 15:50
8.4 Open the file romeo.txt and read it line by line. For each line, split the line into a list of words using the split() function. The program should build a list of words. For each word on each line check to see if the word is already in the list and if not append it to the list. When the program completes, sort and print the resulting words …
fname = raw_input("Enter file name: ")
fh = open(fname)
lst = list() # list for the desired output
for line in fh: # to read every line of file romeo.txt
word= line.rstrip().split() # to eliminate the unwanted blanks and turn the line into a list of words
for element in word: # check every element in word
if element in lst: # if element is repeated
continue # do nothing
else : # else if element is not in the list
lst.append(element) # append