Search code examples
tweepyvalueerror

Invalid literal for int() but converting to float wont work either


I'm creating a tweeter bot but the code won't run because of a conversion to an int literal. Even trying to convert to float creates an error. What's the solution here?

This is the program code

import pandas as pd
import time
import tweepy

appkey= "##############"
appSecret= "##############"
accessToken= "##############"
acessSecret="##############"


latest_tweet_number =0
with open(r'C:\\Users\Kweronda\Desktop\tweek\Quotes.csv') as f: 
    latest_tweet_number = int(f.read())

print(latest_tweet_number)

df = pd.read_csv(r'C:\Users\Kweronda\Desktop\tweek\Quotes.csv', sep =';')

def tweet_post(msg):
    msg = msg[0:270]
    try:
        auth = tweepy.OAuth1UserHandler(appkey, appSecret)
        auth.set_access_token(accessToken, acessSecret)
        api = tweepy.API(auth)
        try:
            api.verify_credentials()
            print('Authentication Ok')
        except:
            print('Error authenticating')

        api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
        api.update_status(msg)
        print('tweet sent')

    except Exception as e:
        print(e)

for idx, rows in df.iterrows():
    if idx <= latest_tweet_number:
        continue

    hashtags = "#inspiration #motivation quotes"
    tweet_post(rows['Quote']+ ' - '+rows['AUTHOR']+ '\n\n\n' +hashtags)

    with open('C:\\Users\Kweronda\Desktop\tweek\latest.txt', "w") as f:
        f.write(str(idx))

    print('done')
    time.sleep(1800)

This is the error;

latest_tweet_number = int(f.read()) ValueError: invalid literal for int() with base 10: 'QUOTE;AUTHOR;GENRE\nAge is an issue of mind over matter. If you don't mind, it doesn't matter.;Mark Twain;age\nAnyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learni


Solution

  • The read method returns the content of the whole file and I guess that your Quotes.csv file is full of quotes, so what are you expecting by trying to cast it to an integer?

    At first sight, I think that you don't need that latest_tweet_number at all.

    And as a side note, you should probably use f-strings to improve readabilty.