簡體   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