国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - python利用文件讀寫編寫一個博客

python利用文件讀寫編寫一個博客

2021-12-23 11:05oceans of *stars Python

這篇文章主要為大家詳細介紹了python利用文件讀寫編寫一個博客,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了python利用文件讀寫編寫一個博客的具體代碼,供大家參考,具體內容如下

代碼展示

import random
import json
import time
import os
 
 
def zhuce():
    print("*********************正在注冊*********************")
    try:
        users = readfile()
    except:
        fa = open(r'test.json', "w",encoding="utf-8")
        fa.write(json.dumps({"初始化": "初始化"}))
        fa.close()
        users = readfile()
        os.makedirs('用戶信息')
    b = 0
    user_name = input("請輸入你的用戶名:")
    user_password = input("請輸入你的密碼:")
    for key in users.keys():
        if user_name == key:
            print("用戶名已存在")
            b = 1
            break
    if b == 0:
        writefile1(user_name)
        users[user_name] = user_password
        writefile1(user_name)
        writefile(users)
    return b
 
 
def readfile():
    f = open(r'test.json', "r+")
    f1 = json.load(f)
    f.close()
    return f1
 
 
def writefile(a):
    a = json.dumps(a)
    f = open(r'test.json', "w")
    f.write(a)
    f.close()
 
 
def homepage():
    print("*********************微博主頁*********************")
    a = input("1注冊 2登錄 3退出\n請輸入你的選擇:")
    return a
 
 
def zhucetexiao():
    print("注冊中,請稍后!")
    print("更新成功!")
    print("注冊成功!")
    print("默認文章表創建完成!")
 
 
def denglu():
    b = 0
    print("*********************正在登錄*********************")
    users = readfile()
    user_name = input("登錄用戶名:")
    user_password = input("登錄密碼:")
    for key in users.keys():
        if user_name == key:
            print("用戶存在,判斷密碼")
            b = 2
            if user_password == users[key]:
                print("登陸成功")
                b = 1
                dengluxiaoguo(user_name)
                break
    if b == 0:
        print("登錄失敗")
    return b
 
 
def dengluxiaoguo(user_name):
    while 0 == 0:
        a = input("1寫文章 2讀取所有 3讀取一篇 4編輯一篇 5刪除一篇 6登出\n請輸入你的選擇:")
        users = article_read(user_name)
        if a == "1":
            print("*********************寫一篇文章*********************")
            article_write(user_name)
        elif a == "2":
            duqusuoyou(user_name)
        elif a == "3":
            print("*********************讀一篇文章*********************")
            duquyipian(user_name)
        elif a == "4":
            print("*********************改一篇文章*********************")
            bianjiyipian(user_name)
        elif a == "5":
            print("*********************刪一篇文章*********************")
            shanchuyipian(user_name)
        elif a == "6":
            break
 
 
def shanchuyipian(user_name):
    duqusuoyou(user_name)
    a = input("請輸入您要刪除的序號?")
    f = open(f'用戶信息\{user_name}.json', "r+")
    f1 = json.load(f)
    f.close()
    if len(f1) > 1:
        if int(a) > 0 and int(a) <= len(f1):
            f1.pop(a)
            for i in range(1, len(f1) + 2):
                if i > int(a):
                    f1[str(i - 1)] = f1.pop(str(i))
            f = open(f'用戶信息\{user_name}.json', "w")
            b = json.dumps(f1)
            f.write(b)
            f.close()
        else:
            print("未找到該序列")
    else:
        print("最后一篇文章拒絕刪除")
    duqusuoyou(user_name)
 
 
def bianjiyipian(user_name):
    duqusuoyou(user_name)
    a = input("請輸入您要編輯的序號?")
    f = open(f'用戶信息\{user_name}.json', "r+")
    f1 = json.load(f)
    f.close()
    if int(a) > 0 and int(a) <= len(f1):
        article_name = input("文章名稱")
        article_content = input("文章內容")
        f = open(f'用戶信息\{user_name}.json', "r+")
        f1 = json.load(f)
        f.close()
        f = open(f'用戶信息\{user_name}.json', "w")
        time1 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        f1[a] = {"title": article_name, "time": time1, "author": user_name, "content": article_content}
        b = json.dumps(f1)
        f.write(b)
        f.close()
    else:
        print("未找到該序列")
 
 
def duquyipian(user_name):
    duqusuoyou(user_name)
    a = input("請輸入您要查看的序號?")
    f = open(f'用戶信息\{user_name}.json', "r+")
    f1 = json.load(f)
    f.close()
    if int(a) > 0 and int(a) <= len(f1):
        print(f1[a])
    else:
        print("未找到該序列")
 
 
def duqusuoyou(user_name):
    print("*********************文章目錄*********************")
    f = open(f'用戶信息\{user_name}.json', "r+")
    f1 = json.load(f)
    f.close()
    for key in f1.keys():
        print(f"{key} {f1[key]['title']}        {f1[key]['time']}")
 
 
def article_read(user_name):
    f = open(f'用戶信息\{user_name}.json', "r+")
    f1 = json.load(f)
    f.close()
    return f1
 
 
def article_write(user_name):
    f = open(f'用戶信息\{user_name}.json', "r+")
    f1 = json.load(f)
    f.close()
    key_count = len(f1) + 1
    article_name = input("文章名稱:")
    article_content = input("文章內容:")
    time1 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    f = open(f'用戶信息\{user_name}.json', "w")
    # print(type(key_count))
    f1[str(key_count)] = {"title": article_name, "time": time1, "author": user_name, "content": article_content}
    print(f1)
    b = json.dumps(f1)
    f.write(b)
    f.close()
 
 
def readfile1():
    f = open(r'test1.json', "r+")
    f1 = json.load(f)
    f.close()
    return f1
 
 
def writefile1(a):
    time1 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    f = open(f'用戶信息\{a}.json', "w")
    b = {1: {"title": "初始化文章", "time": time1, "author": "0", "content": "0"}}
    print(b)
    b = json.dumps(b)
    f.write(b)
    f.close()
 
 
while 0 == 0:
    h = homepage()
    if h == "1":
        if zhuce() == 0:
            zhucetexiao()
    elif h == "2":
        if denglu() == 1:
            pass
    else:
        exit()

文件保存樣式:

python利用文件讀寫編寫一個博客

運行結果:

python利用文件讀寫編寫一個博客

python利用文件讀寫編寫一個博客

python利用文件讀寫編寫一個博客

 python利用文件讀寫編寫一個博客

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/qq_44850339/article/details/119847021

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 亚洲国产精品一区 | 亚洲免费电影一区 | 91视频免费观看 | 欧洲精品久久久久毛片完整版 | 四虎影院网 | 日韩男女视频 | 精品一区二区久久久久久久网站 | 国产成人av在线 | 色a综合 | 欧美一区久久 | 免费黄色av | 国产免费色 | 亚洲国产成人av好男人在线观看 | 香蕉亚洲 | 中文字幕三级 | 成人刺激视频在线 | 亚洲网在线 | 欧美日韩成人 | 在线视频自拍 | 国产天堂| 欧美激情区 | 91av在线视频播放 | 一区二区三区高清 | www.午夜 | 欧美成人免费在线 | 欧美精品在线免费观看 | 国产一区二区三区欧美 | 国产成人精品网站 | 中文字幕一区二区三区乱码图片 | 成人精品一区二区 | 国产精品久久久久久久久久新婚 | 97久久久| 国产亚洲一区二区三区在线观看 | av免费在线观看网站 | 免费看黄色一级视频 | 91精品国产综合久久小仙女陆萱萱 | 亚州国产 | 久久久国产精品一区 | 色版视频在线观看 | 免费成人av网址 | 波多野结衣一区二区三区 |