我正在学习Python,并且作为一个学习项目,我正在开发一个twitter bot。我正在使用Python 3.我使用以下几行来发送推文。删除和更新文本文件中的行 - Python
什么是李小龙最喜欢的饮料? Wataaaaah!
诵读困难的恶魔崇拜者将他的灵魂卖给了圣诞老人。
你用牛排杀死心脏素食吸血鬼。
有一次监狱休息时间,我看到一只侏儒爬上围栏。当他跳下来时,他嘲笑我,我想,这有点居高临下。
这是我的代码,使用Twython鸣叫:
from twython import Twython, TwythonError
import time
APP_KEY = '##########' # Customer Key here
APP_SECRET = '#############' # Customer secret here
OAUTH_TOKEN = '###############' # Access Token here
OAUTH_TOKEN_SECRET = '################' # Access Token Secret here
twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
try:
with open('jokes.txt', 'r+') as file:
buff = file.readlines()
for line in buff:
if len(line)<140:
print ("Tweeting...")
twitter.update_status(status=line)
time.sleep(3)
with open ('jokes.txt', 'r+') as file:
buff.remove(line)
file.writelines(buff)
else:
with open ('jokes.txt', 'r+') as file:
buff.remove(line)
file.writelines(buff)
print ("Skipped line - Char Length Violation")
continue
except TwythonError as e:
print (e)
我想跳过这个拥有超过140个字符与控制台Skipped line - Char Length Violation
在消息上线,然后删除特定的行和更新文件。该脚本通过忽略该行成功鸣叫,但无法打印控制台消息。它也无法从文本文件中删除该行。
我不知道为什么第三行You kill vegetarian vampires with a steak to the heart.
被跳过。
我运行该脚本后,这有什么错我的代码,为什么我的文本文件看起来像这样:
的诵读困难的魔鬼崇拜者,出卖了自己的灵魂圣诞老人。
有一次监狱休息时间,我看到一只侏儒爬上围栏。当他跳下 他嘲笑我,我想,这是一个有点居高临下。 在我身上,我想,这是一个有点居高临下。我认为, 以及有点居高临下。
谢谢。我编辑了代码。 –
修正了'else'块... –