I want to tweet every 240char but not recognized - Python(Tweepy)

Asked

Viewed 32 times

-1

I want the show to read. txt, record the 240char and post the tweet, and jump to the next 240char, as if you were reading and posting each line, but instead of serious line by amount of char. But I can’t identify how, someone could help me?

Code:

# reading 
    
    bookfile = open('book.txt', encoding = 'utf8')
    booklines = bookfile.readlines()
    bookfile.close()
    
    
    # tweet
    
    def tweet():
        for line in booklines:
            try:
                 print(line)
                 if line != '\n':
                     api.update_status(line, tweet_mode = 'extended')
                     sleep(300)
                 else:
                    pass
            except tweepy.TweepError as e:
                print(e.reason)
                sleep(2)
    
    tweet()

1 answer

1


You can use the function textwrap.wrap that will already return to you a list of substrings with the defined size.

from textwrap import wrap

...
booklines = wrap(bookfile.read(), width=240)

for message in booklines:
  # Envia o tweet
  • In case you were, right? Or how? ''bookfile = open('book.txt', encoding = 'utf8') booklines = wrap(bookfile.read(240)) bookfile.close() for message in booklines:'''

  • @Patok I added the parameter I had forgotten...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.