$ cat names.log | sort | uniq | wc -l
#!/usr/bin/env python
import sys
if __name__ == "__main__":
# 初始化一个names的字典,内容为空
# 字典中为name和出现数量的键值对
names = {}
# sys.stdin是一个文件对象。 所有引用于file对象的方法,
# 都可以应用于sys.stdin.
for name in sys.stdin.readlines():
# 每一行都有一个newline字符做结尾
# 我们需要删除它
name = name.strip()
if name in names:
names[name] += 1
else:
names[name] = 1
# 迭代字典,
# 输出名字,空格,接着是该名字出现的数量
for name, count in names.iteritems():
sys.stdout.write("%d\t%s\n" % (count, name))
$ cat names.log | python namescount.py
$ cat names.log | python namescount.py | sort -rn
$ cat names.log | python namescount.py | sort -rn | head -n 5
"email@example.com", "This service is great."
#!/usr/bin/env python
# CSV module that comes with the Python standard library
import csv
import sys
if __name__ == "__main__":
# CSV模块使用一个reader对象作为输入
# 在这个例子中,就是 sys.stdin.
csvfile = csv.reader(sys.stdin)
# 这个脚本必须接收一个参数,指定列的序号
# 使用sys.argv获取参数.
column_number = 0
if len(sys.argv) > 1:
column_number = int(sys.argv[1])
# CSV文件的每一行都是用逗号作为字段的分隔符
for row in csvfile:
print row[column_number]
#!/usr/bin/env python
import smtplib
import sys
GMAIL_SMTP_SERVER = "smtp.gmail.com"
GMAIL_SMTP_PORT = 587
GMAIL_EMAIL = "Your Gmail Email Goes Here"
GMAIL_PASSWORD = "Your Gmail Password Goes Here"
def initialize_smtp_server():
'''
This function initializes and greets the smtp server.
It logs in using the provided credentials and returns
the smtp server object as a result.
'''
smtpserver = smtplib.SMTP(GMAIL_SMTP_SERVER, GMAIL_SMTP_PORT)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
smtpserver.login(GMAIL_EMAIL, GMAIL_PASSWORD)
return smtpserver
def send_thank_you_mail(email):
to_email = email
from_email = GMAIL_EMAIL
subj = "Thanks for being an active commenter"
# The header consists of the To and From and Subject lines
# separated using a newline character
header = "To:%s\nFrom:%s\nSubject:%s \n" % (to_email,
from_email, subj)
# Hard-coded templates are not best practice.
msg_body = """
Hi %s,
Thank you very much for your repeated comments on our service.
The interaction is much appreciated.
Thank You.""" % email
content = header + "\n" + msg_body
smtpserver = initialize_smtp_server()
smtpserver.sendmail(from_email, to_email, content)
smtpserver.close()
if __name__ == "__main__":
# for every line of input.
for email in sys.stdin.readlines():
send_thank_you_mail(email)
$ cat emailcomments.csv | python csvcolumn.py | ?python namescount.py | sort -rn > /tmp/comment_freq $ cat /tmp/comment_freq | head -n 10 | cut -f2 | ?python sendemail.py
#!/usr/bin/env python
import sys
if __name__ == "__main__":
# The first argument of sys.argv is always the filename,
# meaning that the length of system arguments will be
# more than one, when command-line arguments exist.
if len(sys.argv) > 2:
num1 = long(sys.argv[1])
num2 = long(sys.argv[2])
else:
print "This command takes two arguments and adds them"
print "Less than two arguments given."
sys.exit(1)
print "%s" % str(num1 + num2)
#!/usr/bin/env python
import smtplib
import sys
from optparse import OptionParser
def initialize_smtp_server(smtpserver, smtpport, email, pwd):
'''
This function initializes and greets the SMTP server.
It logs in using the provided credentials and returns the
SMTP server object as a result.
'''
smtpserver = smtplib.SMTP(smtpserver, smtpport)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
smtpserver.login(email, pwd)
return smtpserver
def send_thank_you_mail(email, smtpserver):
to_email = email
from_email = GMAIL_EMAIL
subj = "Thanks for being an active commenter"
# The header consists of the To and From and Subject lines
# separated using a newline character.
header = "To:%s\nFrom:%s\nSubject:%s \n" % (to_email,
from_email, subj)
# Hard-coded templates are not best practice.
msg_body = """
Hi %s,
Thank you very much for your repeated comments on our service.
The interaction is much appreciated.
Thank You.""" % email
content = header + "\n" + msg_body
smtpserver.sendmail(from_email, to_email, content)
if __name__ == "__main__":
usage = "usage: %prog [options]"
parser = OptionParser(usage=usage)
parser.add_option("--email", dest="email",
help="email to login to smtp server")
parser.add_option("--pwd", dest="pwd",
help="password to login to smtp server")
parser.add_option("--smtp-server", dest="smtpserver",
help="smtp server url", default="smtp.gmail.com")
parser.add_option("--smtp-port", dest="smtpserverport",
help="smtp server port", default=587)
options, args = parser.parse_args()
if not (options.email or options.pwd):
parser.error("Must provide both an email and a password")
smtpserver = initialize_smtp_server(options.stmpserver,
options.smtpserverport, options.email, options.pwd)
# for every line of input.
for email in sys.stdin.readlines():
send_thank_you_mail(email, smtpserver)
smtpserver.close()
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有