import json def op_data(filename,dic=None): if dic: with open(filename,'w',encoding='utf-8') as fw: json.dump(dic,fw,ensure_ascii=False,indent=4)#json串写入文件中 else: with open(filename,encoding='utf-8') as fr: return json.load(fr)#文件里面内容写入json格式 def add_goods(good): global goods temp = {} thing = input("请输入商品:").strip() nums = input("请输入总量:").strip() prices = input("请输入价格:").strip() colors = input("请输入颜色:").strip() temp['num'] = nums temp['price'] = prices temp['color'] = colors good[thing] = temp return goods FILE_NAME = "goods.json" goods = op_data(FILE_NAME) #print(goods) all_goods = add_goods(goods) op_data(FILE_NAME,all_goods)
json字符串的读与写
猜你喜欢
转载自www.cnblogs.com/ymany/p/8888578.html
今日推荐
周排行