简体   繁体   English

使用 sys.argv[1] 和 numpy 数组将多个 arguments 传递给 Python 脚本。 IndexError:列表索引超出范围

[英]Passing multiple arguments to Python script using sys.argv[1] and numpy array. IndexError: list index out of range

I'm working in a small code to receive multiple arguments from an MQTT server and use them to predict another value.我正在使用一个小代码从 MQTT 服务器接收多个 arguments 并使用它们来预测另一个值。 I'm showing a simplified code here just to get some help.我在这里展示一个简化的代码只是为了获得一些帮助。 To pass the arguments to the script for executing the prediction, first part is the creation of a numpy array, then pass the arguments to the script using sys.argv[], then the indexing to position the incoming values. To pass the arguments to the script for executing the prediction, first part is the creation of a numpy array, then pass the arguments to the script using sys.argv[], then the indexing to position the incoming values.

import numpy as np
import sys

    # creating empty numpy array for feature values
X = np.empty(2).reshape(1, 2)

    #storing the arguments
azimuth_sin=sys.argv[1]
azimuth_cos=sys.argv[2]

    #displaying the arguments
print("azimuth_sin : " + azimuth_sin)
print("azimuth_cos : " + azimuth_cos)
print("Number of arguments : ", len(sys.argv))
   
    # set vector values
X[:,0] = sys.argv[1]
X[:,1] = sys.argv[2]

print(X)

However, I have an issue with the second argument as I get an error:但是,当我收到错误时,我对第二个参数有疑问:

exit code: 1, Traceback (most recent call last): File "numpy-array.py", line 10, in azimuth_cos=sys.argv[2] IndexError: list index out of range退出代码:1,Traceback(最近一次调用):文件“numpy-array.py”,第 10 行,在 azimuth_cos=sys.argv[2] IndexError: list index out of range

The only way to avoid that error is if I set both arguments to: sys.arg[1]避免该错误的唯一方法是如果我将 arguments 都设置为: sys.arg[1]

   #storing the arguments
azimuth_sin=sys.argv[1]
azimuth_cos=sys.argv[1]

   #displaying the arguments
print("azimuth_sin : " + azimuth_sin)
print("azimuth_cos : " + azimuth_cos)
print("Number of arguments : ", len(sys.argv))
   
   # set vector values
X[:,0] = sys.argv[1]
X[:,1] = sys.argv[1]

print(X)

Then I get two consecutive outputs:然后我得到两个连续的输出:

azimuth_sin: -0.9152180545267792 azimuth_cos: -0.9152180545267792 Number of arguments: 2 [[-0.91521805 -0.91521805]] azimuth_sin:-0.9152180545267792 azimuth_cos:-0.9152180545267792 arguments 数量:2 [[-0.91521805 -0.91521805]]

and:和:

azimuth_sin: 0.40295894662883136 azimuth_cos: 0.40295894662883136 Number of arguments: 2 [[0.40295895 0.40295895]] azimuth_sin: 0.40295894662883136 azimuth_cos: 0.40295894662883136 数量 arguments: 2 [[0.40295895 0.40295895]]

which are actually the values of the two arguments printed, but repeated twice: sin = -0.9152180545267792 and cos = 0.40295894662883136这实际上是打印的两个 arguments 的值,但重复了两次: sin = -0.9152180545267792 和cos = 0.40295894662883136

If I put the arguments in one line:如果我将 arguments 放在一行中:

   #storing the arguments
azimuth_sin, azimuth_cos = sys.argv[1:2] 

The error is:错误是:

exit code: 1, Traceback (most recent call last): File "numpy-array-t1.py", line 10, in azimuth_sin, azimuth_cos = sys.argv[1:2] ValueError: not enough values to unpack (expected 2, got 1)退出代码:1,Traceback(最近一次调用最后一次):文件“numpy-array-t1.py”,第 10 行,在 azimuth_sin,azimuth_cos = sys.argv[1:2] ValueError:没有足够的值来解包(预期 2 , 得到 1)

I've tried many ways to fix this without success, I'd appreciate any help or suggestions.我已经尝试了很多方法来解决这个问题,但没有成功,我将不胜感激任何帮助或建议。 Thank you in advance.先感谢您。

Start with something simple to validate the data you are receiving.从简单的事情开始来验证您收到的数据。

Do something like:执行以下操作:

import sys  
  
# Verify first so you don't get an error  
# This check verifies we have at least two parameters
if 1 < len(sys.argv):
    if sys.argv[1]:
        var_argv1 = sys.argv[1]
        print("var_argv1 type: %s, length: %s" % (type(var_argv1), len(var_argv1)))

        # It is pointless to continue if argv[1] has no data
        if sys.argv[2]:
            var_argv2 = sys.argv[2]
            print("var_argv2 type: %s, length: %s" % (type(var_argv2), len(var_argv2)))
        else:
            print("sys.argv[2] has no data")
    else:
        print("sys.argv[1] has no data")

It may be that you are trying to process a numpy object on the command line可能是您尝试在命令行上处理 numpy object

Note:笔记:
Do you have access to the MQTT server?您可以访问 MQTT 服务器吗?
It might be easier to pick a channel (topic) to use for this data transfer.选择用于此数据传输的通道(主题)可能更容易。
You could get the MQTT server to publish this data on a channel and have this script subscribe to that channel.您可以让 MQTT 服务器在频道上发布此数据,并让此脚本订阅该频道。
Then you could make sending information as easy as a function call on your MQTT system.然后,您可以像在 MQTT 系统上调用 function 一样简单地发送信息。

In Linux terminal window, I have a simple script that just displays sys.argv :在 Linux 终端 window 中,我有一个仅显示sys.argv的简单脚本:

1619:~$ cd mypy
1619:~/mypy$ cat echo.py
import sys
print(sys.argv)

When I call it thus:当我这样称呼它时:

1619:~/mypy$ python3 echo.py 1.23 3.112 foo bar
['echo.py', '1.23', '3.112', 'foo', 'bar']

See that sys.argv is list of 5 strings, which come from the commandline.看到sys.argv是来自命令行的 5 个字符串的列表。

If you are calling your script from a shell or windows command window, you should be able to enter and see multiple strings.如果您从 shell 或 windows 命令 window 调用您的脚本,您应该能够输入并查看多个字符串。

But people have problems using sys.argv (and argparse ) when running the script from something like pydev or as a Jupyter notebook.但是当从pydev或 Jupyter 笔记本运行脚本时,人们在使用 sys.argv (和argparse )时会遇到问题。 I don't know anything about the MQTT server so can't help with providing even one command line argument.我对MQTT server一无所知,因此甚至无法提供一个命令行参数。 As I demonstrate, sys.argv is primarily intended as a way of providing startup values and options when running a script from the operating system.正如我所展示的, sys.argv主要用于在从操作系统运行脚本时提供启动值和选项。

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

相关问题 IndexError:列表索引超出范围:sys.argv[1] 超出范围 - IndexError: list index out of range: sys.argv[1] out of range sys.argv[1] 索引错误:列表索引超出范围 - sys.argv[1] IndexError: list index out of range Python GetHostId.py获取sys.argv [1] IndexError:列表索引超出范围 - Python GetHostId.py getting sys.argv[1] IndexError: list index out of range driver.get(sys.argv[1]) 在 PC 上正常工作,而在其他 PC 上工作正常(sys.argv[1],IndexError: list index out of range 错误) - driver.get(sys.argv[1]) working fine on a PC and not on the other (sys.argv[1], IndexError: list index out of range error) Python sys.argv [1]索引超出范围 - Python sys.argv[1] index out of range python sys.argv[1] 列表索引超出范围 - python sys.argv[1] list index out of range Sys.argv python错误列表索引超出范围 - Sys.argv python error list index out of range 使用 sys.argv[1] 时“列表索引超出范围” - “list index out of range” when using sys.argv[1] 我在使用Python命令“ cascPath = sys.argv [1]”时遇到问题,出现错误IndexError:列表索引超出范围 - Iam having trouble with Python command “ cascPath = sys.argv[1] ” i get error IndexError: list index out of range ds = data_set(sys.argv [1])IndexError:列表索引超出范围 - ds = data_set(sys.argv[1]) IndexError: list index out of range
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM