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.768108149999999,“from”:“USD”}
但我有个错误:
TypeError: string indices must be integers, not str
调用的结果不是字典,而是字符串:
看起来结果是一个JSON编码的dict,因此您可以使用
[注意,虽然我认为有更好的方法来处理添加诸如
from
和to
之类的参数,但我还是保留了url构造。还要注意,在这种情况下,将转换率转换为int
是没有意义的相关问题 更多 >
编程相关推荐