我正在尝试执行我的Python脚本:

python series.py supernatural 4 6

Supernatural : TV Series name
4 : season number
6 : episode number

Now in my script I am using the above three arguments to fetch the title of the episode:

import tvrage.api
import sys

a =  sys.argv[1]
b = sys.argv[2]
c =  sys.argv[3]

temp = tvrage.api.Show(a)
name  = temp.season(b).episode(c)  # Line:19
print ( name.title)

但我有个错误:
File "series.py", line 19, in <module>:
  name = super.season(b).episode(c)
File "C:\Python26\Lib\site-packages\tvrage\api.py", line 212, in season
  return self.episodes[n] KeyError: '4'

我正在使用Python2.6。

最佳答案

Python TVRage API需要的是整数,而不是字符串(这是从argv获得的):

name = temp.season(int(b)).episode(int(c))

将纠正错误,如果第4季第6集存在。
您应该看看Python附带的命令行解析模块对于3.2/2.7或更新版本,请使用argparse。对于旧版本,请使用optparse。如果您已经知道cgetopt,请使用getopt

10-07 12:09