Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ArgParse AttributeError:';str';对象没有属性_Python_Argparse - Fatal编程技术网

Python ArgParse AttributeError:';str';对象没有属性

Python ArgParse AttributeError:';str';对象没有属性,python,argparse,Python,Argparse,我已经在Python脚本中实现了argparse,如下所示: parser = argparse.ArgumentParser() parser.add_argument("-s", "--shortterm", help="Tweets top information from the past month", default="track", choices=choices, dest="shortterm") parser.add_argument(

我已经在Python脚本中实现了argparse,如下所示:

parser = argparse.ArgumentParser()
parser.add_argument("-s", "--shortterm", help="Tweets top information from the past month", default="track",
                    choices=choices, dest="shortterm")
parser.add_argument("-m", "--mediumterm", help="Tweets top information from the past 6 months", default="track",
                    choices=choices)
parser.add_argument("-l", "--longterm", help="Tweets top information from the past few years", default="track",
                    choices=choices)
args = parser.parse_args()
然后,我在args中检查用户可能输入或选择的内容,如:

if args.mediumterm:
    if args.mediumterm == "all":
        get_top_tracks(medium_term)
        get_top_artists(medium_term)
    else:
        if args == "track":
            get_top_tracks(medium_term)
        elif args == "artist":
            get_top_artists(medium_term)
使用以下命令运行脚本时:

python top_tracks_artists_spotify_time.py --mediumterm all
我得到以下错误:

Traceback (most recent call last):
File "top_tracks_artists_spotify_time.py", line 127, in <module>
if args.mediumterm:
AttributeError: 'str' object has no attribute 'mediumterm'
成功运行脚本


编辑:我已将dest=“mediumterm”添加到argparse中,但没有任何效果

您的处理代码位于
args=parser之后。parse_args()
应该类似于:

term = args.mediumterm
if term:
    if term == "all":
        get_top_tracks(term)     # unless medium_term is defined else where
        get_top_artists(term)
    else:
        if term == "track":
            get_top_tracks(term)
        elif term == "artist":
            get_top_artists(term)

类似地,对于短期和长期。由
parse\u args
创建后,不应重新分配
args
(这只会混淆您和您的读者)。

您在何处/如何定义
args
print
以查看您要查找的内容:
print(args)
print(args.\uu dict\uuu)
print(dir(args))
@aydow添加了代码。它一直存在,只是没有被错误地复制和粘贴到问题中args在这个上下文中是一个字符串:
args.mediumterm
,所以我不明白这是怎么可能的。在某个时刻,您正在将args从dict/对象转换为字符串。在第127行之前搜索
args=
,并确保您没有覆盖它。这很有意义,而且是一种系统性的更好的方法。谢谢
term = args.mediumterm
if term:
    if term == "all":
        get_top_tracks(term)     # unless medium_term is defined else where
        get_top_artists(term)
    else:
        if term == "track":
            get_top_tracks(term)
        elif term == "artist":
            get_top_artists(term)