对于int()无效,但转换为float也不能工作



我正在创建一个推特机器人,但由于转换为int字面量,代码不会运行。即使尝试转换为float也会产生错误。这里的解是什么?

这是程序代码

import pandas as pd
import time
import tweepy
appkey= "##############"
appSecret= "##############"
accessToken= "##############"
acessSecret="##############"

latest_tweet_number =0
with open(r'C:\UsersKwerondaDesktoptweekQuotes.csv') as f: 
latest_tweet_number = int(f.read())
print(latest_tweet_number)
df = pd.read_csv(r'C:UsersKwerondaDesktoptweekQuotes.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']+ 'nnn' +hashtags)
with open('C:\UsersKwerondaDesktoptweeklatest.txt', "w") as f:
f.write(str(idx))
print('done')
time.sleep(1800)

这是错误;

latest_tweet_number = int(f.read())以10为基数的int()的无效文字:'QUOTE;AUTHOR;GENREnAge是一个思想高于物质的问题。马克·吐温年龄任何一个停止学习的人都已老了,无论是二十岁还是八十岁。坚持学习的人

read方法返回整个文件的内容,我猜你的Quotes.csv文件充满了引号,所以你想通过尝试将其转换为整数来期望什么?

乍一看,我认为你根本不需要latest_tweet_number

作为旁注,您可能应该使用f字符串来提高可读性。

最新更新