错误在Python脚本关于STR(sys.argv中[1]) [英] Error in Python Script regarding str(sys.argv[1])

查看:1498
本文介绍了错误在Python脚本关于STR(sys.argv中[1])的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import MySQLdb as mdb
import math
import sys 
from decimal import Decimal

conn=mdb.connect('localhost','root','','testdb')
c=conn.cursor()

user_id=str(sys.argv[1])
lati=str(sys.argv[2])
longi=str(sys.argv[3])
radius=int(str(sys.argv[4]))

#user_id=1
#lati='28.635308'
#longi='77.224960'
#radius=1.5

该错误是:

Traceback (most recent call last):
  File "C:\Python27\recommend.py", line 9, in <module>
    user_id=str(sys.argv[1])
IndexError: list index out of range

不过这是由#注释的虚拟值工作正常。在解决这个错误任何帮助吗?

However the dummy values which is commented by # are working fine. Any help in resolving this error ?

推荐答案

一个办法检查错误,你可以打印出的 sys.argv中列表,并检查它的元素个数。错误的输出是说,只有一个元素(最有可能是脚本的本身-script名称 - )在 sys.argv中。最有可能当你运行你不提供的参数脚本(您必须提供4个参数)。

One way to checking the mistake you can print out the sys.argv list and check the number of elements it has. The output of the error was saying there is only one element (most probably it's the script's itself -script name-) in the sys.argv. Most probably when you are running the script you don't supply the arguments(you must supply 4 arguments).

这篇关于错误在Python脚本关于STR(sys.argv中[1])的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆