Python Getting ValueError:基数为10的int()的文本无效,尝试使用datanews模块
我试图使用Datanews API,但我得到了一个Python Getting ValueError:基数为10的int()的文本无效,尝试使用datanews模块,python,api,Python,Api,我试图使用Datanews API,但我得到了一个ValueError:-( 这是我的密码: import datanews datanews.api_key = '[API KEY]' response = datanews.headlines(q='Apple', language=['en']) articles = response['hits'] num = 0 for num in articles: nnum = int(str(num)) if nnum
ValueError
:-(
这是我的密码:
import datanews
datanews.api_key = '[API KEY]'
response = datanews.headlines(q='Apple', language=['en'])
articles = response['hits']
num = 0
for num in articles:
nnum = int(str(num))
if nnum != 10:
print(articles[nnum]['title'])
num = nnum + 1
else:
break
错误:
回溯(最近一次呼叫最后一次):
文件“twitter.py”,第12行,在
nnum=int(str(num))
ValueError:以10为基数的int()的文本无效:“{'url\”:\”https://www.theapplepost.com/2020/10/22/apple-discontinues-apple-tv-remote-app/\“,”来源“:”theapplepost.com“,”作者“:[\'theapplepost\”],”标题“:”苹果停止应用程序
当您尝试转换未格式化为整数的字符串值时,会引发PythonValueError:invalid literal for int(),以10为基数出现错误
要解决此问题,可以使用float()
方法将字符串中的浮点数转换为整数。然后,可以使用int()
将数字转换为整数
如果这不起作用,请确保字符串的值不包含任何字母。带字母的字符串不能转换为整数,除非这些字母在Python中具有特殊含义
要正确了解>>使用打印语句进行调试
..............
for num in articles:
print(num) # This will tell you the truth
nnum = int(str(num))
..............
我没有在您的输入中看到任何int
:
“{'url\':\”https://www.theapplepost.com/2020/10/22/apple-discontinues-apple-tv-remote-app/\“,”来源“:”theapplepost.com“,”作者“:[\'theapplepost\'],”标题“:”Apple停止Appl
您尝试将哪个值转换为哪个整数?请使用完整的错误回溯更新您的问题。我尝试将num转换为int
# if you have a list and you want to get index value pair
for i,v in enumerate(yourList)