如何从异常中恢复并从打开的文件中继续行字符串?我被卡住了!
try:
while True:
with open('us.txt') as f:
for user in f:
for tweet in tweepy.Cursor(api.user_timeline, screen_name=user, ).items():
print(tweet.user.screen_name)
csvWriter.writerow(tweet.user.screen_name)
except tweepy.TweepError as e:
print(e.reason)
sys.exit()
答案 0 :(得分:0)
如果您(确实有点令人困惑)的问题是询问如何忽略该异常,但仍然继续您在文件中的位置,则应尝试将try:
和except:
块保留为接近问题线。
例如,如果csvWriter.writerow(tweet.user.screen_name)
是失败的行,您可以执行以下操作:
while True:
with open('us.txt') as f:
for user in f:
for tweet in tweepy.Cursor(api.user_timeline, screen_name=user,).items():
print(tweet.user.screen_name)
try:
csvWriter.writerow(tweet.user.screen_name)
except tweepy.TweepError as e:
print(e)
如果错误出现在for tweet in tweepy.Cursor(...
行,您可以这样做:
while True:
with open('us.txt') as f:
for user in f:
try:
for tweet in tweepy.Cursor(api.user_timeline, screen_name=user,).items():
print(tweet.user.screen_name)
csvWriter.writerow(tweet.user.screen_name)
except tweepy.TweepError as e:
print(e)
我希望这有帮助!