![](/img/trans.png)
[英]I keep getting this error: ValueError: invalid literal for int() with base 10: ''
[英]I keep getting ValueError: invalid literal for int() with base 10: ''
这是代码,但每次都会创建此错误。 ValueError: int() 以 10 为底的无效文字:''
这是主要代码。 是为了发推文
import tweepy
import pandas
import time
import creds
latest_tweeet_id =0
appkey= creds.appkey
appSecret = creds.appSecret
accessToken = creds.accessToken
acessSecret = creds.acessSecret
latest_tweeet_id =0
with open('latest.txt', 'r') as f:
latest_tweeet_id = int(f.read())
print(latest_tweeet_id)
df = pandas.read_csv('quotes.csv', sep=';')
def tweet(msg):
msg = msg[0:270]
try:
auth = tweepy.OAuth1UserHandler(creds.appkey, creds.appSecret, creds.accessToken, creds.acessSecret)
api = tweepy.API(auth)
try:
api.verify_credentials()
print("Auth Ok")
except:
print('Error')
api = tweepy.API(auth, wait_on_rate_limit=True)
api.update_status(msg)
print(msg)
except Exception as e:
print(e)
for idx, rows in df.iterrows():
if idx <= latest_tweeet_id:
continue
tweet(rows["QUOTE"])
with open('latest.txt', 'w') as f:
f.write(str(idx))
这是错误:
Traceback (most recent call last): wtflax2 (master)
File "app.py", line 15, in <module>
latest_tweeet_id = int(f.read())
ValueError: invalid literal for int() with base 10: ''
latest.txt 文件只有一位数字,1。
问题是文件latest.txt
是空的。 当 python 读取文件时,它会获取空字符串,当它尝试将空字符串解释为 integer 时,它会给您该错误。
latest.txt 文件只有一位数字,1。
错误信息明确表示从latest.txt
读取的值是空字符串,这意味着文件是空的。 也许问题是您从哪里运行程序? Python 查找的latest.txt
文件将位于您从中运行 Python 的目录中。 如果您有两个latest.txt
文件,并且您正在从包含空文件的目录中运行 Python,这可以解释您所看到的内容。
或者您可能只是在编辑后忘记保存latest.txt
? 这样的事情在我身上发生过好几次了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.