先看数据层
(这一节留言不再是一串字符串,而是一列字典:{"id": 编号, "text": 内容, "by": 作者};数据存在一个 JSON 文件里,应用启动时读、改动后写回——project_06 会把它换成数据库。)
按 id 查、按 id 删、删后再查:
ROWS = [{"id": 1, "text": "第一条留言", "by": "小明"}, {"id": 2, "text": "今天天气不错", "by": "小红"}, {"id": 3, "text": "天气预报说明天下雨", "by": "小明"}, {"id": 4, "text": "<b>不许</b>", "by": "路人"}]
def find(rows, i):
for r in rows:
if r["id"] == i:
return r
return None
def remove(rows, i):
r = find(rows, i)
if r:
rows.remove(r)
return r is not None
rows = [dict(r) for r in ROWS]
a = find(rows, 2)["text"]
ok = remove(rows, 1)
b = find(rows, 2)["text"]
print(a + "/" + str(ok) + "/" + b + "/" + str(remove(rows, 1)) + "/" + str([r["id"] for r in rows]).replace(" ", ""))
全部评论