Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.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-列表索引超出范围?推特回复_Python_List_Twitter_Tweepy - Fatal编程技术网

Python-列表索引超出范围?推特回复

Python-列表索引超出范围?推特回复,python,list,twitter,tweepy,Python,List,Twitter,Tweepy,我一直在编写一个脚本,在日志中删除一条推文的回复 现在,我还没有独自完成这项工作,并最终使它几乎可以工作,但我几乎在最后发现了一个索引错误,“列表索引超出范围” 我有点困惑,因为我不知道这里有什么问题。。。有人能解释一下吗 def tweet_url(t): return "https://twitter.com/%s/status/%s" % (t.user.screen_name, t.id) def get_tweets(filename): for line in op

我一直在编写一个脚本,在日志中删除一条推文的回复

现在,我还没有独自完成这项工作,并最终使它几乎可以工作,但我几乎在最后发现了一个索引错误,“列表索引超出范围”

我有点困惑,因为我不知道这里有什么问题。。。有人能解释一下吗

def tweet_url(t):
    return "https://twitter.com/%s/status/%s" % (t.user.screen_name, t.id)

def get_tweets(filename):
    for line in open(filename):
        yield twitter.Status.NewFromJsonDict(json.loads(line))

def get_replies(tweet):
    user = tweet.user.screen_name
    tweet_id = tweet.id
    max_id = None
    logging.info("looking for replies to: %s" % tweet_url(tweet))
    while True:
        q = urllib.parse.urlencode({"q": "to:%s" % user})
        try:
            replies = t.GetSearch(raw_query=q, since_id=tweet_id, max_id=max_id, count=100)
        except twitter.error.TwitterError as e:
            logging.error("caught twitter api error: %s", e)
            time.sleep(60)
            continue
        for reply in replies:
            logging.info("examining: %s" % tweet_url(reply))
            if reply.in_reply_to_status_id == tweet_id:
                logging.info("found reply: %s" % tweet_url(reply))
                yield reply
                # recursive magic to also get the replies to this reply
                for reply_to_reply in get_replies(reply):
                    yield reply_to_reply
            max_id = reply.id
        if len(replies) != 100:
            break

if __name__ == "__main__":
    logging.basicConfig(filename="replies.log", level=logging.INFO)
    tweets_file = sys.argv[1] 
    for tweet in get_tweets(tweets_file):
        for reply in get_replies(tweet):
            print(reply.AsJsonString())

所以。。。在底线上,列表(sys.argv[1])导致了这里的问题,但我不明白为什么会出现超出范围的索引错误,知道吗?

sys.argv
指传递给脚本的命令行参数。运行脚本时,
sys.argv[0]
将是脚本的名称
sys.argv[1]
将是第一个参数,
sys.argv[2]
将是第二个参数,等等。脚本希望
sys.argv[1]
将是存储结果的文件名。如果未提供此选项,列表
sys.argv
的长度将为1,索引
[1]
将超出范围。尝试使用

python中的
script.py output.txt

-

传递给Python脚本的命令行参数列表。argv[0]是脚本名(这取决于操作系统是否为完整路径名)

如果我读这篇文章,我会一直读到这一点-

传递给Python脚本的命令行参数列表

这意味着,
sys.argv
是一个列表,当您试图从列表中不存在的内容(按索引)中访问时,它会为您提供一个
索引器。您需要使用脚本所需的参数调用脚本,这些参数将从
sys.argv[1]

比如说-

python file_name.py some_argument
可以从
sys.argv[1]
访问一些参数。您可以通过在argv-like上使用
try
len
来测试参数是否已传递给脚本-

try:
    args = sys.argv[1]
except IndexError:
    print('No argument passed')
或-


它需要脚本中包含一个命令行参数

如果从命令行运行,请使用以下命令:

python script.py tweet_file.txt
在哪里,

argv[0]被视为脚本名-script.py

arg[1]是tweet文件名


执行时缺少tweet文件名。从代码中,我猜可能是一些包含tweets的文件

你能发布完整的错误信息吗?谢谢!就这样!没问题@SantiagoGuinand
python script.py tweet_file.txt