我希望字典保持与找到日期的顺序相同,因此字典按日期排序。我看了this python site但是代码不起作用我在尝试使用这个类时遇到错误。任何想法为什么以及如何解决它?
page = str('<OPTION VALUE="08/25/2013" >08/25/2013</OPTION><OPTIONVALUE="09/01/2013">09/01/2013</OPTION><OPTION VALUE="09/08/2013" >09/08/2013</OPTION><OPTION VALUE="09/15/2013" >09/15/2013</OPTION><OPTION VALUE="09/22/2013" >09/22/2013</OPTION><OPTION VALUE="09/29/2013" >09/29/2013</OPTION><OPTION VALUE="10/06/2013" >10/06/2013</OPTION><OPTION VALUE="10/13/2013" >10/13/2013</OPTION><OPTION VALUE="10/20/2013">10/20/2013</OPTION><OPTIONVALUE="10/27/2013">10/27/2013</OPTION><OPTION VALUE="11/03/2013" >11/03/2013</OPTION><OPTION VALUE="11/10/2013" >11/10/2013</OPTION><OPTION VALUE="11/17/2013" >11/17/2013</OPTION><OPTION VALUE="11/24/2013" >11/24/2013</OPTION><OPTION VALUE="12/01/2013" >12/01/2013</OPTION><OPTION VALUE="12/08/2013" >12/08/2013</OPTION><OPTION VALUE="12/15/2013" >12/15/2013</OPTION>OPTION VALUE="12/22/2013" >12/22/2013</OPTION><OPTION VALUE="12/29/2013" >12/29/2013</OPTION><OPTION VALUE="01/05/2014" >01/05/2014</OPTION><OPTION VALUE="01/12/2014" >01/12/2014</OPTION><OPTION VALUE="01/19/2014" >01/19/2014</OPTION><OPTION VALUE="01/26/2014" >01/26/2014</OPTION><OPTION VALUE="02/02/2014" >02/02/2014</OPTION><OPTION VALUE="02/09/2014" >02/09/2014</OPTION><OPTION VALUE="02/16/2014" >02/16/2014</OPTION><OPTION VALUE="02/23/2014" >02/23/2014</OPTION><OPTION VALUE="03/02/2014" >03/02/2014</OPTION><OPTION VALUE="03/09/2014" >03/09/2014</OPTION><OPTION VALUE="03/16/2014" >03/16/2014</OPTION><OPTION VALUE="03/23/2014" >03/23/2014</OPTION><OPTION VALUE="03/30/2014" >03/30/2014</OPTION><OPTION VALUE="04/06/2014" >04/06/2014</OPTION><OPTION VALUE="04/13/2014" >04/13/2014</OPTION><OPTION VALUE="04/20/2014" >04/20/2014</OPTION><OPTION VALUE="04/27/2014" >04/27/2014</OPTION><OPTION VALUE="05/04/2014" >05/04/2014</OPTION><OPTION VALUE="05/11/2014" >05/11/2014</OPTION><OPTION VALUE="05/18/2014" >05/18/2014</OPTION><OPTION VALUE="05/25/2014" >05/25/2014</OPTION><OPTION VALUE="06/01/2014" >06/01/2014</OPTION><OPTION VALUE="06/08/2014" >06/08/2014</OPTION><OPTION VALUE="06/15/2014" >06/15/2014</OPTION>')
def web_link (enter_web_link):
#11%2F10%2F2013
enter_web_link = enter_web_link.replace("/","%") #00%00%0000
add_twoF = enter_web_link[:3]+"2F"+ enter_web_link[3:] #00%2F00%0000
add_twoF_everywhere = add_twoF[:8] +"2F"+add_twoF[8:]
add_twoF_everywhere = str(add_twoF_everywhere)
return add_twoF_everywhere
def search_13(page):
starter = '<OPTION VALUE="' # find the postion where this starts
start_link = page.find(starter)
starter = len(starter)
if start_link == -1:
return None, 0, None
start_link = start_link + starter
end_date = start_link + 10
datetext = page[start_link: end_date]
str_date = str(datetext) #this is hte actuall normal looking date dd/mm/yyyy
enter_web_link = str_date # this will enter we_link function to change to percent signs
endoflinkdate = web_link(enter_web_link)
return str_date , end_date, endoflinkdate
def getalllinks(page):
links = {}
while True:
str_date,end_date,endoflinkdate = search_13(page)
if str_date:
links[str_date] ='link' + endoflinkdate
page = page[end_date:]
else:
break
return links
work = getalllinks(page)
print work
class OrderedCounter(Counter, OrderedDict): #THIS DOES NOT WORK CAUSES ERRORS
def __repr__(self):
return '%s(%r)' % (self.__class__.__name__, OrderedDict(self))
def __reduce__(self):
return self.__class__, (OrderedDict(self),)
var1 = OrderedCounter(page,links)
print var1
错误:
Traceback (most recent call last):
File "schedule.py", line 43, in <module>
class OrderedCounter(Counter, OrderedDict):
NameError: name 'Counter' is not defined
怎么没有定义它的参数?
我想把它作为字典作为{date:link,date:link}我可以得到这个,但它与变量页面中日期的顺序不一样。我希望订单与变量页面中的日期相同,所以08/25/2013:link,09/01/2013:link,
我想将它保存在字典中,因为我将其粘贴到javascript网站中,这样我就可以输入日期并获取链接,并且还有一个有序的向下滚动菜单。
答案 0 :(得分:2)
如果要使用Python库中的内容,则需要先导入库:
from collections import OrderedDict, Counter