【发布时间】:2013-03-13 15:04:52
【问题描述】:
import urllib2
currency = 'EURO'
req = urllib2.urlopen(' http://rate-exchange.appspot.com/currency?from=USD&to='+ currency +'')
result = req.read()
print p
p = result["rate"]
print int(p)
这是我用print p 得到的
结果 = {“to”:“EURO”,“rate”:0.76810814999999999,“from”:“USD”}
但我有错误:
TypeError: string indices must be integers, not str
【问题讨论】:
-
不,不是你没有的代码。您发布的示例确实 not 给出了错误。您可能在代码中的某个地方做了一个
result = somestringvalue,而您错过了。 -
该代码在 Python 2.7 中对我来说很好
-
什么版本的python?
-
@ValekHalfHeart:在 Python 3 中,该代码将引发语法错误(因为
print是一个函数)。在 Python 2 中,代码 Just Works。在这里要求一个版本没有意义。 -
是的,确认所有所说的:print int(p) 打印 0,正确。那是你得到的所有代码sn-p吗?
标签: python dictionary