尝试通过推文检查是否已经存在 twython
Trying to go through tweets checking if one already exists twython
for tweets in ExistingTweets:
ExistString = tweets['text']
ExistString = ExistString[:15]
if randomQuote[:16] == ExistString:
randomQuote = AllQuotes[randint(0,5)].getText()
randomQuote = randomQuote[printUntil:]
我想看看我要发布的报价是否已经发布了。 ExistString 和 randomQuote 匹配一次(我知道我在 randomQuote 中调用 :16,但不知何故 :15 和 :16 等于相同的输出),但 randomQuote 没有改变!
感谢任何帮助,谢谢!
你的代码结构有点混乱(我想你没有正确缩进?)我不确定像 printUntil 这样的东西是用来做什么的,但我的建议是像这样:
randomQuote = AllQuotes[randint(0,5)].getText() # Pick your first random quote
while True:
already_used = False
for tweets in ExistingTweets:
if randomQuote[:15] == tweets['text'][:15]:
already_used = True
break
if already_used:
randomQuote = AllQuotes[randint(0,5)].getText() # Pick another and try again.
else:
break
print(randomQuote)
如果你有一大串你已经发过推文的引述文本,这会容易得多;那么你可以直接使用 if randomQuote in ExistingTweets
作为你的测试,根本不需要 for
循环。
正如另一位评论者所说,您不应该使用不同的值([:15]
和 [:16]
)- 在某些特定情况下(字符串短于 17)它们可能会给出相同的结果例如字符),但在其他情况下不会。
for tweets in ExistingTweets:
ExistString = tweets['text']
ExistString = ExistString[:15]
if randomQuote[:16] == ExistString:
randomQuote = AllQuotes[randint(0,5)].getText()
randomQuote = randomQuote[printUntil:]
我想看看我要发布的报价是否已经发布了。 ExistString 和 randomQuote 匹配一次(我知道我在 randomQuote 中调用 :16,但不知何故 :15 和 :16 等于相同的输出),但 randomQuote 没有改变!
感谢任何帮助,谢谢!
你的代码结构有点混乱(我想你没有正确缩进?)我不确定像 printUntil 这样的东西是用来做什么的,但我的建议是像这样:
randomQuote = AllQuotes[randint(0,5)].getText() # Pick your first random quote
while True:
already_used = False
for tweets in ExistingTweets:
if randomQuote[:15] == tweets['text'][:15]:
already_used = True
break
if already_used:
randomQuote = AllQuotes[randint(0,5)].getText() # Pick another and try again.
else:
break
print(randomQuote)
如果你有一大串你已经发过推文的引述文本,这会容易得多;那么你可以直接使用 if randomQuote in ExistingTweets
作为你的测试,根本不需要 for
循环。
正如另一位评论者所说,您不应该使用不同的值([:15]
和 [:16]
)- 在某些特定情况下(字符串短于 17)它们可能会给出相同的结果例如字符),但在其他情况下不会。