#coding:utf-8
"""
python核心编程第9章11题a的答案
本答案由python视频培训班授课老师-黄哥所写。
做这样的习题关键得有思路,参加python视频培训班的一个学员,对这种题目无从下手。
黄哥简单说一下思路:用list或dict来组织数据,文件记录数据,无穷循环+break来
控制文本菜单。由于本人时间有限,代码有进一步完善的必要,请感兴趣者,进一步完善。
也可以用字典来组织数据。
9-11.
Web site Addresses.
Write a URL bookmark manager. Create a text-driven menu-based application that allows the user to add, update, or delete entries.
Entries include a site name, Web site URL address,
and perhaps a one-line description (optional).
Allow search functionality so that a search "word" looks
through both names and URLs for possible matches.
Store the data to a disk file when the user quits the application,
and load up the data when the user restarts.
python视频培训班
形式:自己看视频+作业+答疑,利用业余时间学习。视频50讲,视频中详细的讲解解题思路,课后大量习题
训练学员自己写代码,通过训练达到不光能看懂代码,而且能自己写代码的目的。
目标:
第一目标是:通过学习python学会编程 。
第二目标:会写爬虫的基础部分,网页采集。
第三目表:学会web开发
学习周期:三个月到6个月
咨询:qq:1465376564 电话:010-68165761
http://www.qy7788.com.cn/shiyongxinxi/shiyongxinxi193.html
"""
import re
import cPickle
from os.path import exists
def addUrl(urllist,new):
urllist.append(new)
def deleteUrl(urllist,new):
"""删除"""
urllist.remove(new)
def findUrl(urllist,webname='',url=''):
"""查询"""
f = open("url.dat")
storelist = cPickle.load(f)
f.close()
# print storelist
for item in list(storelist):
if webname in item[0] and url in item[1]:
return item
def writeUrl(urllist):
"""写文件"""
f = open(r"url.dat","w")
cPickle.dump(urllist,f)
f.close()
def readUrl():
"""读文件,先检测文件存在不?"""
if exists('url.dat'):
f = open("url.dat")
storelist = cPickle.load(f)
f.close()
# print storelist
for item in storelist:
for urlitem in item:
print urlitem
urllist = []
#将文件内部的原始信息读到urllist内
if exists('url.dat'):
f = open("url.dat")
storelist = cPickle.load(f)
f.close()
urllist .extend(storelist)
print (30 * '-')
print (" M A I N - M E N U")
print (30 * '-')
print ("1. 增加")
print ("2. 删除")
print ("3. 查询")
print ("4. 写文件")
print ("5. 读文件")
print ("6. 退出")
print (30 * '-')
while True:
try :
choice = int ( raw_input('Enter your choice [1-6] : ') )
except ValueError, e :
print ("'%s' is not a valid integer." % e.args[0].split(": ")[1])
# choice = int ( raw_input('Enter your choice [1-3] : ') )
if choice == 1:
print ("Starting 增加")
webname = raw_input("请输入站名:")
url = raw_input("请输入网址:")
content = raw_input("请输入描述:")
# urllist.append([webname,url,context])
addUrl(urllist,[webname,url,content])
elif choice == 2:
print ("删除...")
webname = raw_input("请输入站名:")
url = raw_input("请输入网址:")
content = raw_input("请输入描述:")
deleteUrl(urllist,[webname,url,content])
elif choice == 3:
print ("查询...")
webname =raw_input("请输入站名:")
url = raw_input("请输入网址:")
# print str(webname)
chaxun = findUrl(urllist,webname,url)
if chaxun:
print "查询结果为:"
for item in chaxun :
print item
else:
print "没有此记录!"
elif choice == 4:
print ("写文件...")
writeUrl(urllist)
elif choice == 5:
print ("读文件...")
readUrl()
elif choice == 6:
print ("退出...")
break
else:
print ("非法内容. Try again...")