如何在python脚本中传递变量

时间:2017-11-28 20:29:56

标签: python function variables

我有一个使用两个函数的python脚本,但其中一个函数不能按预期工作。 main()调用第一个函数Define_and_Collect_1_WireData():,这可以按预期工作。但是,我无法将函数data_stream_logfile中的字符串变量Define_and_Collect_1_WireData():传递给函数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()

7 个答案:

答案 0 :(得分:3)

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

import { NAVIGATE_BACK, NAVIGATE_PROFILE, NAVIGATE_FEED } from '../Actions/types';

const firstAction = AppNavigator.router.getActionForPathAndParams('Main');
const initialNavState = AppNavigator.router.getStateForAction(
  firstAction
);


function nav(state = initialNavState, action) {
  console.log(action.type);
  let nextState;
  switch (action.type) {
    case NAVIGATE_BACK:
      nextState = AppNavigator.router.getStateForAction(
                  NavigationActions.back(),
                  state
                );
      break;

所以,总是,总是,总是尽力不使用全局变量!!!!

答案 1 :(得分:2)

您需要在data_stream_logfile函数中返回Define_and_Collect_1_WireData的值,并使用该值将其传递给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)

答案 2 :(得分:1)

#!/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()

答案 3 :(得分:1)

您可以写信给本地data_stream_logfile并将其返回给来电者。然后拨打log_file(data_stream_logfile)

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

或者您可以将其设为全局变量,以便从您的两个函数进行访问。请记住将global data_stream_logfile添加到您的每个功能中。

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

答案 4 :(得分:1)

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

答案 5 :(得分:0)

您应该在功能中使用return valuesparameters。例如,您可以按如下方式重写脚本:

#!/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()

答案 6 :(得分:-1)

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

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

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

希望我能提供帮助:3