import tweepy
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
def chunks(lst, n):
"""Yield successive n-sized chunks from lst.
from here: https://stackoverflow.com/a/312464/42346"""
for i in range(0, len(lst), n):
yield lst[i:i + n]
with open('to_be_tweeted.txt','r') as f:
for line in f:
split_on_spaces = line.rstrip('\n').split()
chunked = [chunk for chunk in chunks(split_on_spaces,3)]
multiline_tweet = "\n".join([" ".join(word for word in chunk)
for chunk in chunked])
api.update_status(multiline_tweet)
例如:
s = """I'm going to press enter every three words just to annoy you."""
split_on_spaces = s.rstrip('\n').split()
chunked = [chunk for chunk in chunks(split_on_spaces,3)]
multiline_tweet = "\n".join([" ".join(word for word in chunk)
for chunk in chunked])
print(multiline_tweet)
结果:
I'm going to
press enter every
three words just
to annoy you.
在文本编辑器中键入
\n
时,它们不是换行字符,而是Python代码中的换行字符下面的代码将把你的一行推文分成每行三个字,每三个字之间有一个换行符
例如:
结果:
相关问题 更多 >
编程相关推荐