如何将所有参数转换为字符串格式的字符串
从 Corey Goldberg 的这个开始:
#!/usr/bin/env python
import json
import pprint
import urllib2
def get_stock_quote(ticker_symbol):
url = 'http://finance.google.com/finance/info?q=%s' % ticker_symbol
lines = urllib2.urlopen(url).read().splitlines()
return json.loads(''.join([x for x in lines if x not in ('// [', ']')]))
if __name__ == '__main__':
quote = get_stock_quote('IBM')
print 'ticker: %s' % quote['t']
print 'current price: %s' % quote['l_cur']
print 'last trade: %s' % quote['lt']
print 'full quote:'
pprint.pprint(quote)
使用这个:
import urllib2, json
def get_stock_quote(ticker_symbol):
url = 'http://finance.google.com/finance/info?q=%s' % ticker_symbol
lines = urllib2.urlopen(url).read().splitlines()
#print lines
return json.loads(''.join([x for x in lines if x not in ('// [', ']')]))
if __name__ == '__main__':
symbols = ('Goog',)
symbols2 = ('Goog','MSFT')
quote = get_stock_quote(symbols)
print 'ticker: %s' % quote['t'], 'current price: %s' % quote['l_cur'], 'last trade: %s' % quote['ltt']
print quote['t'], quote['l'], quote['ltt']
使用符号有效,符号2无效。错误消息为
TypeError:在字符串格式化期间未转换所有参数
如何将所有参数转换为字符串格式中的字符串。在浏览器中,有效的代码是:Goog,MSFT。
编辑:我正在寻找的输出是一个包含 goog、msft 信息的列表。
Starting with this from Corey Goldberg:
#!/usr/bin/env python
import json
import pprint
import urllib2
def get_stock_quote(ticker_symbol):
url = 'http://finance.google.com/finance/info?q=%s' % ticker_symbol
lines = urllib2.urlopen(url).read().splitlines()
return json.loads(''.join([x for x in lines if x not in ('// [', ']')]))
if __name__ == '__main__':
quote = get_stock_quote('IBM')
print 'ticker: %s' % quote['t']
print 'current price: %s' % quote['l_cur']
print 'last trade: %s' % quote['lt']
print 'full quote:'
pprint.pprint(quote)
Using this:
import urllib2, json
def get_stock_quote(ticker_symbol):
url = 'http://finance.google.com/finance/info?q=%s' % ticker_symbol
lines = urllib2.urlopen(url).read().splitlines()
#print lines
return json.loads(''.join([x for x in lines if x not in ('// [', ']')]))
if __name__ == '__main__':
symbols = ('Goog',)
symbols2 = ('Goog','MSFT')
quote = get_stock_quote(symbols)
print 'ticker: %s' % quote['t'], 'current price: %s' % quote['l_cur'], 'last trade: %s' % quote['ltt']
print quote['t'], quote['l'], quote['ltt']
Usings symbols works, symbols2 does not work. The error message is
TypeError: not all arguments converted during string formatting
How do I convert all arguments to string in string formatting. In browser, the code that works is: Goog,MSFT.
EDIT: the output I am looking for is a list with goog, msft info.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的问题是 %s 无法处理包含 2 个元素的元组,请参阅下面的示例:
无论如何,请使用
format()
而不是%
。Your problem is that %s can't handle a tuple with 2 elements, see that example below:
Anyway, use
format()
instead of%
.在这种情况下,您可以:
In this case you can make:
您收到此错误是因为格式化只需要一个字符串,而您将元组放入 2 个字符串。如果您想获取 http://finance.google.com/finance/info? q=Goog,MSFT 你应该这样做
或者循环执行此操作:
You got this error because formatting require only one string, while you put tuple with 2 strings. If you want to get http://finance.google.com/finance/info?q=Goog,MSFT you should do
Or do this in cycle:
问题就在这里:
使用
symbols2
,您提供的是 2 元组,而格式化运算符需要标量或 1 元组。以下内容将修复立即错误:
这只能解决一半的问题:解析结果的代码也需要更改。我将其作为练习留给读者。
The problem is here:
With
symbols2
, you're supplying a 2-tuple whereas the formatting operator expects a scalar or a 1-tuple.The following will fix the immediate error:
This only fixes half of the problem: the code that parses the results will also need to change. I leave this as an exercise for the reader.