我最近开始学习 Python,我有一个它的应用程序,我想创建一个文本文件,每两秒记录一次数据,持续一分钟,然后重新开始一个新文件。我把它写在打开的文件为 SQL 格式化的地方,并且正在添加 SQL INSERT 查询。
我遇到的问题是创建代码以实际正常工作。 Python 本身可以很好地向已打开的文件插入一行新的 SQL,但输出如下所示:
[blank line is here because of the \n]
INSERT INTO table (column 1, column 2) VALUES ('7113', '1337');
INSERT INTO table (column 1, column 2) VALUES ('7113', '1337');
我的问题是我需要获取数据,因此顶部没有新行,最后一行没有分号。
这是我的来源:
from threading import Timer
import time
def start():
t = Timer(1.0,start)
foo = "7113"
bar = "1337"
date = time.strftime('%Y-%m-%d %H:%M:%S')
sql = "\nINSERT INTO table (column 1, column 2) VALUES ('%s', '%s');" % (foo, bar)
query = open(date + ".sql", "a")
query.write(sql)
query.close()
t.start()
start()
由于我的语言不是最好的,所以我想做的更简单的版本是:
- 让一个函数运行第一个代码,吐出
INSERT INTO table (column 1, column 2) VALUES ('7113', '1337');
前面没有额外的行,但有一个分号。 - 让另一个函数运行 58 次,在 SQL 查询之前添加带有
\n
的行,然后添加分号。 - 让最后一个函数运行第 60 次,包括
\n
但没有分号。 - 关闭当前文件并生成一个包含该分钟时间戳的新文本文件,例如
2013-07-09 12-30-00.sql
并为重复该过程>2013-07-09 12-31-00.sql
等
我想在那之后还有更多,比如让 PHP 或 Python 脚本读取日期并执行它,或者我可以让所有这些脚本都执行,然后按升序排列表格。但是,这可以在以后解决和研究。我只是想让它工作,因为我已经搜索了几个小时如何做到这一点,但我找不到任何有用的东西。不过,我确信答案是显而易见的。
感谢阅读,希望对大家有所帮助!我现在经常使用这个网站来获取有用的花絮,这太棒了。想出这个主意的人都应该受到表扬。
此外,如果有人有任何有用的学习 Python 的资源(我是通过 Codecademy 学习的),我将不胜感激。这比学习 C++ 有趣得多。
最佳答案
我通过使用 global counting
变量并使用 if
和 elif
语句解决了这个问题。目前该脚本每 500 毫秒运行一次,没有问题。如果有人想看我的代码,这里是:
Commented version because syntax highlighting here with comments is screwing up for whatever reason.
from threading import Timer
import xml.etree.ElementTree as ET
import urllib
import time
datetime = time.strftime('%Y-%m-%d %H.%M')
filename = datetime + ".sql"
url = "localhost/send.cgi"
interval = 0.5
table_name = "0001"
columns = "(time, foo, bar)"
counting = 0
def parser():
t = Timer(interval, parser)
global counting
global filename
query = open(filename, "a")
if counting == 0:
date = int(time.time()*1000)
web = urllib.urlopen(url)
tree = ET.parse(web)
root = tree.getroot()
foo = root[1].text.replace(" ", "")
bar = root[2].text.replace(" ", "")
sql = "INSERT INTO %s %s VALUES ('%s', '%s', '%s');" % (table_name, columns, date, foo, bar)
counting += 1
query.write(sql)
print counting
t.start()
elif (counting > 0) and (counting < 59):
date = int(time.time()*1000)
web = urllib.urlopen(url)
tree = ET.parse(web)
root = tree.getroot()
foo = root[1].text.replace(" ", "")
bar = root[2].text.replace(" ", "")
sql = "\nINSERT INTO %s %s VALUES ('%s', '%s', '%s');" % (table_name, columns, date, foo, bar)
counting += 1
query.write(sql)
print counting
t.start()
elif counting == 59:
date = int(time.time()*1000)
web = urllib.urlopen(url)
tree = ET.parse(web)
root = tree.getroot()
foo = root[1].text.replace(" ", "")
bar = root[2].text.replace(" ", "")
sql = "\nINSERT INTO %s %s VALUES ('%s', '%s', '%s')" % (table_name, columns, date, foo, bar)
counting += 1
query.write(sql)
print counting
t.start()
elif counting == 60:
query.close()
datetime = time.strftime('%Y-%m-%d %H-%M')
filename = datetime + ".sql"
counting = 0
t.start()
parser()
如果可以做出任何改进,我很乐意看到它们! <3 不过,此代码适用于某种网络服务器。它可能需要一些修改才能工作。 Web 服务器所做的是,在收到请求时发送一个虚拟 XML 文件(只是数据,而不是文件),此代码请求它、解释它并将其发送到一个 .sql 文件中,以便我稍后可以将它们执行到一个表的东西。最后,代码将抓取十多个不同的列数据并创建一个小时的文件,即 7200 半秒。
尽情享受吧!
编辑:是的,显然 SQL 语法不起作用。故障排除。
关于python - 每秒运行 Python 函数一分钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17555638/