【发布时间】:2010-10-16 06:05:12
【问题描述】:
我正在学习 Python 作为我的第二种编程语言(如果不计算 HTML/CSS/Javascript,我将学习第一种真正的编程语言)。我正在尝试构建一些有用的东西作为我的第一个真正的应用程序 - 一个 IRC 机器人,当频道中发生某些事情时,它会通过 SMS 提醒人们。根据某人的请求,我正在(尝试)建立日程安排偏好,人们可以选择在一天中的 X 和 Y 小时之间不接收警报。
无论如何,这是我遇到问题的代码:
db = open("db.csv")
for line in db:
row = line.split(",") # storing stuff in a CSV, reading out of it
recipient = row[0] # who the SMS is going to
s = row[1] # gets the first hour of the "no alert" time range
f = row[2] # gets last hour of above
nrt = [] # empty array that will store hours
curtime = time.strftime("%H") # current hour
if s == "no":
print "They always want alerts, sending email" # start time will = "no" if they always want alerts
# send mail code goes here
else:
for hour in range(int(s), int(f)): #takes start, end hours, loops through to get hours in between, stores them in the above list
nrt.append(hour)
if curtime in nrt: # best way I could find of doing this, probably a better way, like I said I'm new
print "They don't want an alert during the current hour, not sending" # <== what it says
else:
# they do want an alert during the current hour, send an email
# send mail code here
我遇到的唯一问题是脚本最终只能循环通过其中一行(或类似的内容),因为我每次只能得到一个结果,即使我在 CSV 文件中有多个条目.
【问题讨论】:
-
一般来说,我不会从头开始编写 IRC 机器人,我也会尝试阻止其他人这样做。为 supybot 或 gozerbot 编写插件。
-
它根本不是一个 IRC 机器人,它所做的只是使用套接字,加入一个房间,并在它看到 x 发生时触发“发送消息”功能。换句话说,我不需要 Supybot 的所有功能。
-
你确定程序没有失败或抛出异常吗?
-
是的,它执行成功,没有错误。
-
为什么不使用现有的 IRC 机器人/框架,为什么不使用 csv 模块?