使用Python

时间:2015-10-17 11:58:46

标签: python regex string function

我正在编写一个Python脚本来读取一个文本文件,该文件每小时更新一次,从中包含一些数据然后写入csv文件。我可以将数据分区并写入csv没问题。我正在尝试实现为每天制作新的csv文件的能力,以便文件保持足够小以供人阅读并允许轻松搜索过去的数据。我还想用日期命名该csv。这一部分再次起作用。我的脚本读取文本文件的时间戳并将其解析为字符串。我还有一个解析的时间字符串保存到文本文件,我可以比较。我遇到的问题是,即使日期相同且时间相同,我的脚本仍会将相同的数据写入csv而不是退出脚本,直到下次在cron下运行。我认为问题的一行是第16行。我觉得这是问题,因为当它比较两个字符串时,应该发现它们是相同的,脚本应该退出。但是,它不会这样做并继续编写脚本。这可能不是问题,真正的问题可能是其他地方,但我没有足够的经验来找到这个缺陷。我已经研究了这个问题超过一个星期,并试图搜索我能想到的每一个可能的问题,但没有人解决这个问题。任何帮助将非常感激。我想我的所有评论都很好,可以进行同行评审,如果我需要更好地解释一下,请告诉我。

def date_check(new_date, save_date): # checks if date is the same
    if new_date != save_date: # if dates are not the same the call new_csv to make new csv
        new_csv(new_date) # pass date string of new file for file name  
    if new_date == save_date: # if the dates are the same then call time check      
        time_check(new_time, save_time) # pass variables to compare

def time_check(new_time, save_time): # function to check if time is the same    
    if new_time != save_time: # if times are not same then parce the data
        parcer(time_string, data) # pass time_string and data string to be appended 
    if new_time == save_time: # if times are the same then(same file) exit program
        sys.exit() # exit program completely, THIS PART DOES NOT WORK

1 个答案:

答案 0 :(得分:0)

由于这是google for#34;退出嵌套函数python"的第一搜索结果,标题中问题的答案是:

使用sys.exit(),例如

import sys

def foo():
    print("Loading bar")
    while(True):
        bar()

def bar():
    sys.exit()

def main():
    print("Loading foo")
    while(True):
        foo()

if __name__ == '__main__':
    main()

我假设OP解决了他们的问题,因为这是在一年前开放的,如果有人遇到类似的东西,看起来new_time永远不会等于save_time,所以sys.exit()永远不会被评估。