Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python退出嵌套函数_Python_Regex_String_Function - Fatal编程技术网

使用Python退出嵌套函数

使用Python退出嵌套函数,python,regex,string,function,Python,Regex,String,Function,我正在编写一个Python脚本来读取一个每小时更新一次的文本文件,从中打包一些数据,然后写入csv文件。我可以将数据打包并写入csv,没有问题。我正在尝试实现为每天创建一个新的csv文件的功能,这样文件就足够小,可以让人阅读,并且可以方便地搜索过去的数据。我还想用日期命名该csv。这一部分同样有效。我的脚本读取文本文件的时间戳并将其解析为字符串。我还将一个解析后的时间字符串保存到一个文本文件中,以便进行比较。我的问题是,即使日期和时间相同,我的脚本仍会将相同的数据写入csv,而不是退出脚本,直到

我正在编写一个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
由于这是谷歌搜索“退出嵌套函数python”的#1搜索结果,因此标题中问题的答案是:

使用sys.exit(),例如


我认为OP解决了他们的问题,因为它是在一年前打开的。如果有人遇到类似的问题,那么新的时间永远不等于节省时间,因此sys.exit()永远不会得到评估。

请创建一个新的。在我看来,你应该删除那些让人分心的序言(解释整个应用程序的第一句话)。此外,您还谈到了第16行,但您只提供了12行代码的一部分。为什么将其标记为regex?请去掉这个问题的regex标签。你导入了系统模块吗?我导入了系统模块。我有一个函数调用另一个函数。当满足条件时,我使用“sys.exit()”退出脚本。但当它到达那一行时,它似乎没有执行。我可以使用sys.ext it来结束嵌套函数中的脚本吗?我在上面的第二行代码中做了更多的探索,找到了问题的真正根源。布尔值“new_date”和“save_date”各包含一个日期字符串。它们都有相同的字符串,但第3行仍然每次运行。有没有更好的方法来比较布尔人?
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()