【发布时间】:2012-12-13 19:51:23
【问题描述】:
找到了这个关于如何检查字符串列表是否在一行内的好答案 How to check if a line has one of the strings in a list?
但是尝试用字典中的键做类似的事情似乎对我来说不起作用:
import urllib2
url_info = urllib2.urlopen('http://rss.timegenie.com/forex.xml')
currencies = {"DKK": [], "SEK": []}
print currencies.keys()
testCounter = 0
for line in url_info:
if any(countryCode in line for countryCode in currencies.keys()):
testCounter += 1
if "DKK" in line or "SEK" in line:
print line
print "testCounter is %i and should be 2 - if not debug the code" % (testCounter)
输出:
['SEK', 'DKK']
<code>DKK</code>
<code>SEK</code>
testCounter is 377 and should be 2 - if not debug the code
认为也许我的问题是因为 .keys() 给了我一个数组而不是一个列表。但还没有弄清楚如何转换它。 .
【问题讨论】:
-
我刚刚运行了这个,testCounter 是 2,而不是 377。如果遇到意外匹配,我建议在计算匹配时打印出当前行。
-
countryCode in currencies在功能上等同于countryCode in currencies.keys()。
标签: python rss python-2.7