2013-07-09 42 views
1

我最近开始学习Python,我有一个应用程序,我想创建一个文本文件,每两秒钟记录一次数据,然后重新开始一个新文件。我已经写好了将打开的文件格式化为SQL并且正在添加SQL INSERT查询的地方。运行Python函数每秒钟一分钟

我遇到的麻烦是创建代码实际正常工作。 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() 

由于我不是最好的语言用的是我想做一个简单的版本是:

  1. 有一个功能运行第一个代码,吐出INSERT INTO table (column 1, column 2) VALUES ('7113', '1337');前面没有额外的行,但用分号。
  2. 有另外一个函数运行58次,在SQL查询之前加上\n行,之后加上分号。
  3. 是否有最后一个函数运行第60次,包括\n但没有分号。
  4. 关闭当前文件并生成一个新的带有时间戳的文本文件,例如2013-07-09 12-30-00.sql,然后重复2013-07-09 12-31-00.sql等操作。

我想在此之后还有很多事情,比如让PHP或Python脚本读取日期并执行它,或者我可以让它们全部执行,然后按升序排列表。但是,这可以在以后解决和研究。我只是想让这个工作,因为我一直在寻找几个小时,如何做到这一点,我找不到任何有用的东西。不过,我相信答案是显而易见的。

感谢您的阅读,我希望你们能帮忙!我已经使用这个网站了一些有用的花絮,这是太棒了。无论谁提出这个需要赞美一堆。此外,如果任何人有任何有用的学习Python资源(我通过Codecademy了解到),我会非常感激他们。这比学习C++更有趣。

+0

为什么在第一行和最后一行需要不同的行为?为什么不直接用分号和换行符结束每一行? – user2357112

+0

我以为你不能在MySQL中这样做。 – Jake

回答

0

我通过使用global counting变量并使用ifelif语句解决了该问题。目前该脚本每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服务器。它可能需要一些修改才能工作。 Web服务器根据请求发送一个虚拟的XML文件(这只是数据,而不是文件),并且这个代码请求它,解释它并将它发送到一个.sql文件中,以便我稍后可以将它们执行到一个表到某物。最后,代码将抓取十个不同的数据列,并创建一个小时的文件,即7200个半秒。

享受!

编辑:耶,显然SQL语法不起作用。故障排除。