2014年8月27日 星期三

% operator after a string

The % operator after a string is used to combine a string with variables. The %operator will replace a %s in the string with the string variable that comes after it.

string_1 = "Camelot"
string_2 = "place"

print "Let's not go to %s. 'Tis a silly %s." % (string_1, string_2)



name = raw_input("What is your name?")
quest = raw_input("What is your quest?")
color = raw_input("What is your favorite color?")

print "Ah, so your name is %s, your quest is %s, " \
"and your favorite color is %s."   %(name, quest, color)

Date and Time

from datetime import datetime
now = datetime.now()
print now.month
print now.day
print now.year

Output
8
27
2014

2014年8月26日 星期二

count_times

name = raw_input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
lst = list()
hour = list()
counts = dict()
timelst = list()
newlst = list()

for line in handle:
    if line.startswith('From'):
        words = line.split()
        if words[0] == 'From': # take out From, get rid of From: line
            lst.append(words[5]) # take time out of list
     
for hr in lst:
    timelst.append(hr[0:2]) # take hour out of time

for hour in timelst:
    counts[hour] = counts.get(hour, 0) + 1

for key, val in counts.items():
    newlst.append((key, val))

newlst.sort()

for val, key in newlst:
     print val, key

Output:
04 3
06 1
07 1
09 2
10 3
11 6
14 1
15 2
16 4
17 2
18 1
19 1


2014年7月28日 星期一

Dictionary using

name = raw_input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
counts = dict()
lines = ''
words = list()
for line in handle:
    line = line.rstrip()
    if line.startswith('From:'):
        line = line.split()
        lines += line[1] + ' ' # Add only all emails to lines, get rid of From:
words = lines.split()
for word in words:
    counts[word] = counts.get(word,0) + 1  # Create dict and count at the same time.
print counts  
bigcount = None
bigword = None
for word,count in counts.items():
    if bigcount == None or count > bigcount:
        bigword = word
        bigcount = count
print bigword + ' ' + str(bigcount)
     

Output:
cwen@iupui.edu 5

2014年7月26日 星期六

Parse file

fname = raw_input("Enter file name: ")
if len(fname) < 1 : fname = "mbox-short.txt"

fh = open(fname)
count = 0
for line in fh:
    line = line.rstrip(
    if line.startswith('From:'):
        line = line.split()
        print line[1]
        count += 1
   
print "There were", count, "lines in the file with From as the first word"

Output:
stephen.marquard@uct.ac.za
louis@media.berkeley.edu
zqian@umich.edu
rjlowe@iupui.edu
zqian@umich.edu
rjlowe@iupui.edu
cwen@iupui.edu
cwen@iupui.edu
gsilver@umich.edu
gsilver@umich.edu
zqian@umich.edu
gsilver@umich.edu
wagnermr@iupui.edu
zqian@umich.edu
antranig@caret.cam.ac.uk
gopal.ramasammycook@gmail.com
david.horwitz@uct.ac.za
david.horwitz@uct.ac.za
david.horwitz@uct.ac.za
david.horwitz@uct.ac.za
stephen.marquard@uct.ac.za
louis@media.berkeley.edu
louis@media.berkeley.edu
ray@media.berkeley.edu
cwen@iupui.edu
cwen@iupui.edu
cwen@iupui.edu
There were 27 lines in the file with From as the first word