功能不会返回正确的值

时间:2017-04-18 16:38:40

标签: python function return

我现在几个小时都遇到了这个问题。

我编写了一个Python代码来读取和转换文本文件中的数据,一切运行正常。

simulink_robot_motor1=[]
with open('C:\\Users\...\sensor_data.txt',"r") as data_file:
    rows=0
    for line in data_file:
        rows=rows+1
    columns=len(line.split(","))
    simulink_robot_motor=[[0 for x in range(columns)] for y in range(rows)]
    i=0
    with open('C:\\Users\...\sensor_data.txt',"r") as data_file:
        for line in data_file:
            current_line = line.split(",")
            current_line = list(map(float, current_line))
            simulink_robot_motor[i]=current_line
            i=i+1

我对simulink_robot_motor变量感兴趣,该变量具有以下结果:

[[0.0, 3.6],
[1.6e-06, 3.6],
[4.57e-06, 3.6],
[7.67e-06, 3.6],
[1.09e-05, 3.6],
...

现在,我想在函数中使用此代码。因此,如果我调用该函数,则应返回列表simulink_robot_motor

def get_matlab_sensor_data():        
    simulink_robot_motor1=[]
    with open('C:\\Users\...\sensor_data.txt',"r") as data_file:
        rows=0
        for line in data_file:
            rows=rows+1
        columns=len(line.split(","))
        simulink_robot_motor=[[0 for x in range(columns)] for y in range(rows)]
        i=0
        with open('C:\\Users\...\sensor_data.txt',"r") as data_file:
            for line in data_file:
                current_line = line.split(",")
                current_line = list(map(float, current_line))
                simulink_robot_motor[i]=current_line
                i=i+1
                return (simulink_robot_motor)

但如果我运行get_matlab_sensor_data(),我会得到以下结果:

   [[0, 3.6],
   [0, 0],
   [0, 0],
   [0, 0],
   [0, 0],
    ...

我尝试了较小的数据集,也关闭了科学小数风格。但是,它仍然没有奏效。我的循环没有正常运行吗?

2 个答案:

答案 0 :(得分:1)

你的return语句处于不正确的缩进级别,所以它在for循环中执行得太快,永远不会完成循环。

def get_matlab_sensor_data():        
    simulink_robot_motor1=[]
    with open('C:\\Users\...\sensor_data.txt',"r") as data_file:
        rows=0
        for line in data_file:
            rows=rows+1
            columns=len(line.split(","))
        simulink_robot_motor=[[0 for x in range(columns)] for y in range(rows)]
        i=0
        with open('C:\\Users\...\sensor_data.txt',"r") as data_file:
            for line in data_file:
                current_line = line.split(",")
                current_line = list(map(float, current_line))
                simulink_robot_motor[i]=current_line
                i=i+1
    return (simulink_robot_motor)

此外,您可能不需要使用语句缩进第二个:

def get_matlab_sensor_data():        
    simulink_robot_motor1=[]
    with open('C:\\Users\...\sensor_data.txt',"r") as data_file:
        rows=0
        for line in data_file:
            rows=rows+1
            columns=len(line.split(","))
        simulink_robot_motor=[[0 for x in range(columns)] for y in range(rows)]
        i=0
    with open('C:\\Users\...\sensor_data.txt',"r") as data_file:
         for line in data_file:
             current_line = line.split(",")
             current_line = list(map(float, current_line))
             simulink_robot_motor[i]=current_line
             i=i+1
    return (simulink_robot_motor)

答案 1 :(得分:0)

首先,在您的原始代码中,您正在读取整个文件两次,这不是必需的。这是一个简化:

simulink_robot_motor

当您尝试将代码转换为函数时,有两个问题,一个是return成为函数外部不存在的局部变量。第二个是你在第二个for line in data_file:循环中有一个def get_matlab_sensor_data(): results=[] with open(filename,'r') as data_file: for line in data_file: current_line = list(map(float, line.split(','))) results.append(current_line) return results simulink_robot_motor = get_matlab_sensor_data() print(simulink_robot_motor) # -> same results as before 语句,这意味着它只会在读取一行后返回。

以下修复了这两个问题,并展示了如何使用新功能:

JObject