【发布时间】:2012-11-11 03:16:36
【问题描述】:
我使用以下程序创建了从网站获取的城市列表。现在我想从我创建的列表中找到城市(参数)的名称。我该怎么做?
换句话说,我如何从列表中找到一个对象?我试过了:listOfCities.find (city),我收到一个错误,因为找不到属性 find。
def weatherNow (city):
import urllib
connection = urllib.urlopen("http://weather.canoe.ca/Weather/World.html")
weather = connection.read()
connection.close()
cityLoc = weather.find('class="weatherred"')
cityEnd = weather.find("</a>", cityLoc)
if city != -1:
listOfCities = []
while cityLoc != -1:
cityNames = weather[cityLoc+19:cityEnd-1]
listOfCities.append(cityNames)
cityLoc = weather.find('class="weatherred"', cityLoc+1)
cityEnd = weather.find("</a>", cityLoc)
print listOfCities
【问题讨论】:
-
我们在说什么语言??
-
listOfCities长什么样子?可以举个例子吗? -
不要将
camelCase用于变量。见pep-8