繁体   English   中英

如何在python脚本中传递变量

[英]How to pass variables in python script

我有一个 python 脚本,它使用两个函数,但其​​中一个函数无法正常工作。 main() 调用第一个函数Define_and_Collect_1_WireData():这按预期工作。 但是,我无法将函数Define_and_Collect_1_WireData():的字符串变量data_stream_logfile传递给函数log_file()

我没有使用return因为我不确定这会使变量保持局部而不是全局。

我尝试了本网站上提供的许多组合/变体:我无法理解的任何组合/变体都有效。

我怎样才能做到这一点?

#!/usr/bin/env python

import time

# Create a useful time stamp for logging to the log file 
timestamp = time.strftime("%d/%m/%Y %H:%M:%S")


def Define_and_Collect_1_WireData():
    tfile = open("/sys/bus/w1/devices/28-0000052c29e1/w1_slave")
    text = tfile.read()
    tfile.close()
    temperature_data = text.split()[-1]
    temperature = float(temperature_data[2:])
    temperature1 = round(temperature / 1000,1)

    #for the purposes of testing. Temperature1 will be pulled from the Dallas 1-wire bus 
    temperature1=13.1

    # Create data_stream_logfile string
    data_stream_logfile = str(temperature1) + "," + str(timestamp)

    # Print output to a terminal
    print "Raw temperature data as written to log file:"
    print data_stream_logfile
    print " "

def log_file():
    # Open the text log datafile
    datafile_temperature_log = open("log_cron_temperature_data.log", "a", 1)
    datafile_temperature_log.write(data_stream_logfile + "\n")
    datafile_temperature_log.close()



def main():

    Define_and_Collect_1_WireData()

    log_file()

main()

您应该从第一个函数返回它并将其传递给第二个函数:

def Define_and_Collect_1_WireData():
    # stuff...

    # Create data_stream_logfile string
    data_stream_logfile = str(temperature1) + "," + str(timestamp)

    # More stuff
    return data_stream_logfile

def log_file(data_stream_logfile):
    # Open the text log datafile
    datafile_temperature_log = open("log_cron_temperature_data.log", "a", 1)
    datafile_temperature_log.write(data_stream_logfile + "\n")
    datafile_temperature_log.close()



def main():

    data_stream_logfile = Define_and_Collect_1_WireData()

    log_file(data_stream_logfile)

main()

所以,永远,永远,永远尽量不要使用全局变量!!!!

您需要在Define_and_Collect_1_WireData函数中返回data_stream_logfile的值,并将其传递给log_file()

def Define_and_Collect_1_WireData():
  ...
  data_stream_logfile = str(temperature1) + "," + str(timestamp)
  return data_stream_logfile

def log_file(data_stream):
  ...

def main():
    data_stream = Define_and_Collect_1_WireData()
    log_file(data_stream)
#!/usr/bin/env python

import time

# Create a useful time stamp for logging to the log file 
timestamp = time.strftime("%d/%m/%Y %H:%M:%S")


def Define_and_Collect_1_WireData():
    tfile = open("/sys/bus/w1/devices/28-0000052c29e1/w1_slave")
    text = tfile.read()
    tfile.close()
    temperature_data = text.split()[-1]
    temperature = float(temperature_data[2:])
    temperature1 = round(temperature / 1000,1)

    #for the purposes of testing. Temperature1 will be pulled from the Dallas 1-wire bus 
    temperature1=13.1

    # Create data_stream_logfile string
    data_stream_logfile = str(temperature1) + "," + str(timestamp)

    # Print output to a terminal
    print "Raw temperature data as written to log file:"
    print data_stream_logfile
    print " "

    return data_stream_logfile

def log_file(data_stream_logfile):
    # Open the text log datafile
    datafile_temperature_log = open("log_cron_temperature_data.log", "a", 1)
    datafile_temperature_log.write(data_stream_logfile + "\n")
    datafile_temperature_log.close()



def main():

    log_file (Define_and_Collect_1_WireData())

main()

您可以写入本地data_stream_logfile并将其返回给调用者。 然后调用log_file(data_stream_logfile)

或者您可以在第一个函数中使用参数data_stream_logfile调用log_file()

或者您可以将其设置为全局变量以从您的两个函数中访问。 请记住将global data_stream_logfile放入您的每个函数中。

最后一种是不鼓励的,因为它容易出错。

这是范围的问题。 当您调用DaC1WD (因为这是一个非常长的函数名),它会在其中为日志创建一个变量。 当函数完成时,它里面的所有东西都丢失了。 您需要做的是return日志文件,然后将日志文件作为参数调用记录器。

您应该在函数中使用返回值参数 例如,您可以按如下方式重写脚本:

#!/usr/bin/env python

import time

# Create a useful time stamp for logging to the log file 
timestamp = time.strftime("%d/%m/%Y %H:%M:%S")


def define_and_collect_1_wire_data():
    tfile = open("/sys/bus/w1/devices/28-0000052c29e1/w1_slave")
    text = tfile.read()
    tfile.close()
    temperature_data = text.split()[-1]
    temperature = float(temperature_data[2:])
    temperature1 = round(temperature / 1000,1)

    #for the purposes of testing. Temperature1 will be pulled from the Dallas 1-wire bus 
    temperature1=13.1

    # Create data_stream_logfile string
    data_stream_logfile = str(temperature1) + "," + str(timestamp)

    # Print output to a terminal
    print "Raw temperature data as written to log file:"
    print data_stream_logfile
    print " "
    return data_stream_logfile

def log(message):
    # Open the text log datafile
    datafile_temperature_log = open("log_cron_temperature_data.log", "a", 1)
    datafile_temperature_log.write(message + "\n")
    datafile_temperature_log.close()



def main():

    log_file(define_and_collect_1_wire_data())

main()

您必须将变量设为全局变量,因为它当前是该特定函数的本地变量。

global data_stream_logfile
data_stream_logfile = str(temperature1) + "," + str(timestamp)

这将使您可以在程序中的任何其他地方使用它

希望我能帮上忙:3

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM