zoukankan      html  css  js  c++  java
  • 增删改查程序


    import json
    import pprint
    import string
    import os

    #输入
    choice = input("请输入1:添加商品 2:修改商品 3:查询商品 4:删除商品:")
    #根据输入的字段,判断行为
    dict={}

    #读取文件
    def read_file(filename):
    f_read=open(filename,'r',encoding='utf-8')
    dict=json.load(f_read)
    print(dict)
    f_read.close()
    return dict


    #写文件
    def write_file(filename,content):
    f_write=open(filename,'w',encoding='utf-8')
    f_write.writelines(content)

    f_write.close()

    #判断输入是否为中文
    def chinese_name(word):
    for ch in word:
    if 'u4e00' <= ch <= 'u9fff':
    return True
    return False #判断没有走对的,直接报错

    #判断价格是否为小数
    def is_float(s):
    s=str(s)
    if s.count('.')==1:
    new_s=s.split('.')
    left_num=new_s[0]
    right_num=new_s[1]
    if right_num.isdigit():
    if left_num.isdigit():
    return True
    elif left_num.count('-')==1 and left_num.startswith('-'):
    tmp_num=left_num.split('-')[-1]
    if tmp_num.isdigit():
    return True

    return False

    #写入颜色和价格
    def color_price():

    color_name = input("请输入颜色:", ).strip()
    price_money = input("请输入价格:").strip()

    # 如果用户名和密码都符合条件
    if chinese_name(color_name) and is_float(price_money):
    # 插入用户名和密码
    dict1[name] = {}
    print(dict1)
    pprint.pprint(dict1[name])
    print(type(dict1[name]))
    dict1[name]['color'] = color_name
    dict1[name]['price'] = price_money
    print(dict1)
    # 转换成json字符串,格式化
    read_replace(dict1)
    # 如果用户名不符合条件
    elif not chinese_name(color_name) and is_float(price_money):
    print("用户名不符合条件!")
    elif chinese_name(color_name) and not is_float(price_money):
    print("密码不符合条件!")
    else:
    print("用户名/密码都不符合条件!")

    #写入文件且期换
    def read_replace(content):
    content=json.dumps(dict1,ensure_ascii=False,indent=4)
    write_file('b2.txt', content)
    os.remove('b.txt')
    os.rename('b2.txt', 'b.txt')
    #判断输入的是否为数字
    while True:
    if choice.isdigit():

    #添加商品
    if choice=='1':

    dict1=read_file('b.txt')

    name=input("请输入商品名称:").strip()
    #字典中没有名字
    if name not in dict1:
    color_price()
    else:
    print("已存在!")
    break



    #修改商品
    elif choice=='2':
    dict1 = read_file('b.txt')
    print(3)
    name = input("请输入商品名称:").strip()
    #字典中有名字
    if name in dict1:
    print("修改")
    color_price()
    break

    #查询商品
    elif choice=='3':
    dict1 = read_file('b.txt')
    name = input("请输入商品名称:").strip()
    if name =="全部":
    print(dict1)
    break
    else:
    print(dict1[name])
    break

    #删除商品
    elif choice=='4':
    dict1 = read_file('b.txt')
    name = input("请输入商品名称:").strip()
    if name in dict1:
    print("删除")
    del dict1[name]
    read_replace(dict1)
    break

  • 相关阅读:
    聪明的质检员 (二分)
    分巧克力(二分)
    产生冠军 HDU
    Legal or Not HDU
    确定比赛名次 HDU
    最短路径问题 HDU
    dijkstra算法为什么不能有负边?
    最短路 HDU
    dijkstra算法 模板
    Floyd算法模板--详解
  • 原文地址:https://www.cnblogs.com/Mustardseed/p/11587468.html
Copyright © 2011-2022 走看看