@chenwei123
2018-06-17T14:41:17.000000Z
字数 5491
阅读 523
冰橙公司
输入一节课钻石的最大值(
maxZuan)、最小值(minZuan),经验的最大值(maxGrow)、最小值(minGrow)。指明 json 文件的读路径,写路径。就行了
#!/usr/bin/env python3# -*-coding:utf-8 -*-#chmod a+x hello.py ,这样可以直接以./hello.py 的形式运行文件import jsonimport timeimport codecsimport loggingimport random#存储 json文件def store(data):with open('/Users/chen/Desktop/python3/data/pythonL52.json', 'wb') as json_file:json_file.write(json.dumps(data, sort_keys=True, indent=4, ensure_ascii=False).encode('utf8'))#读取 json 文件def openFile(filePath):with open(filePath, 'r') as f:data = json.load(f)return data#统计一节课中 action 的次数def countCourseAction(courseData):#判断最后一条消息是不是习题if "exercises" in courseData[-1]:#数组末尾追加一条打卡信息dic = {"record":True, "action":"打卡炫耀下", "message":"打卡炫耀下"}courseData.append(dic)else:courseData[-1]["record"] = Trueif "action" not in courseData[-1]:courseData[-1]["action"] = "打卡炫耀下"actionCount = 0for msg in courseData:#python2.7 has_key, python3.5 if key in dictif "exercises" in msg:# if msg.has_key('exercises'):wrongArray = msg["wrong"]for wrongMsg in wrongArray:if "action" in wrongMsg:actionCount +=1correctArray = msg["correct"]for correctMsg in correctArray:if "action" in correctMsg:actionCount +=1elif "action" in msg:actionCount +=1return actionCount#chapter,tag 编码,zuan_number,grow_number 赋值def operationCourseData(key, courseData, minZuan, maxZuan, minGrow, maxGrow):chapter = 1tag = 1for msg in courseData:#python2.7 has_key, python3.5 if key in dictzuanNum = random.randint(minZuan, maxZuan)growNum = random.randint(minGrow, maxGrow)if "exercises" in msg:if "tag" in msg:msg["tag"] = key +"-"+str(tag)tag += 1wrongArray = msg["wrong"]for wrongMsg in wrongArray:if "action" in wrongMsg:wrongMsg["chapter"] = chapterwrongMsg["zuan_number"] = 0wrongMsg["grow_number"] = 0chapter += 1correctArray = msg["correct"]i=0for correctMsg in correctArray:if "action" in correctMsg:correctMsg["chapter"] = chaptercorrectMsg["zuan_number"] = zuanNumcorrectMsg["grow_number"] = growNumchapter += 1i=i+1elif i==0 and "action" not in correctMsg:correctMsg["action"] = "下一条"correctMsg["chapter"] = chaptercorrectMsg["zuan_number"] = zuanNumcorrectMsg["grow_number"] = growNumchapter += 1i=i+1elif "action" in msg:if "record" in msg:msg["chapter"] = chaptermsg["zuan_number"] = 0msg["grow_number"] = 0chapter += 1else:msg["chapter"] = chaptermsg["zuan_number"] = zuanNummsg["grow_number"] = growNumchapter += 1if __name__ == "__main__":data = openFile('/Users/chen/Desktop/python3/data/pythonL5.json')# print(data)# maxZuan = input("输入一节钻石的最大数:")# minZuan = input("输入一节钻石的最小数:")# maxGrow = input("输入一节经验的最大数:")# minGrow = input("输入一节经验的最小数:")maxZuan = 50minZuan = 25maxGrow = 250minGrow = 130for key in data.keys():if key == "catalogs":continueprint("第", key, "节")courseData = data[key] #节数据[]actionCount = countCourseAction(courseData)print("action 的次数:",actionCount)#添加钻石,经验minZu = int(int(minZuan)/actionCount)maxZu = int(int(maxZuan)/actionCount)minGr = int(int(minGrow)/actionCount)maxGr = int(int(maxGrow)/actionCount)print("钻石:", minZu, "~", maxZu)print("经验:", minGr, "~", maxGr)operationCourseData(key, courseData, minZu, maxZu, minGr, maxGr)# print(data)store(data)
#!/usr/bin/env python# -*-coding:utf-8 -*-#chmod a+x hello.py ,这样可以直接以./hello.py 的形式运行文件import jsonimport timeimport codecsimport loggingimport random#存储 json文件def store(data):with open('/Users/chen/Desktop/python3/data/data1.json', 'wb') as json_file:json_file.write(json.dumps(data, sort_keys=True, indent=4, ensure_ascii=False).encode('utf8'))#读取 json 文件def openFile(filePath):with open(filePath, 'r') as f:data = json.load(f)return data#统计一节课中 action 的次数def countCourseAction(courseData):#判断最后一条消息是不是习题if "exercises" in courseData[-1]:#数组末尾追加一条打卡信息dic = {"record":True, "action":u"打卡炫耀下", "message":u"打卡炫耀下"}courseData.append(dic)else:courseData[-1]["record"] = Trueif "action" not in courseData[-1]:courseData[-1]["action"] = u"打卡炫耀下"actionCount = 0for msg in courseData:#python2.7 has_key, python3.5 if key in dictif "exercises" in msg:#if msg.has_key('exercises'):wrongArray = msg["wrong"]for wrongMsg in wrongArray:if "action" in wrongMsg:actionCount +=1correctArray = msg["correct"]for correctMsg in correctArray:if "action" in correctMsg:actionCount +=1elif "action" in msg:actionCount +=1return actionCount#chapter,tag 编码,zuan_number,grow_number 赋值def operationCourseData(key, courseData, minZuan, maxZuan, minGrow, maxGrow):chapter = 1tag = 1for msg in courseData:#python2.7 has_key, python3.5 if key in dictzuanNum = random.randint(minZuan, maxZuan)growNum = random.randint(minGrow, maxGrow)if "exercises" in msg:if "tag" in msg:msg["tag"] = key +"-"+str(tag)tag += 1wrongArray = msg["wrong"]for wrongMsg in wrongArray:if "action" in wrongMsg:wrongMsg["chapter"] = chapterwrongMsg["zuan_number"] = 0wrongMsg["grow_number"] = 0chapter += 1correctArray = msg["correct"]for correctMsg in correctArray:if "action" in correctMsg:correctMsg["chapter"] = chaptercorrectMsg["zuan_number"] = zuanNumcorrectMsg["grow_number"] = growNumchapter += 1elif "action" in msg:if "record" in msg:msg["chapter"] = chaptermsg["zuan_number"] = 0msg["grow_number"] = 0chapter += 1else:msg["chapter"] = chaptermsg["zuan_number"] = zuanNummsg["grow_number"] = growNumchapter += 1if __name__ == "__main__":data = openFile('/Users/chen/Desktop/python3/data/data.json')#print(data)#maxZuan = raw_input(u"输入一节钻石的最大数:")#minZuan = raw_input(u"输入一节钻石的最小数:")#maxGrow = raw_input(u"输入一节经验的最大数:")#minGrow = raw_input(u"输入一节经验的最小数:")maxZuan = 300minZuan = 200maxGrow = 500minGrow = 400for key in data.keys():if key == "catalogs":continueprint u"第", key, u"节"courseData = data[key] #节数据[]actionCount = countCourseAction(courseData)print u"action 的次数:",actionCount#添加钻石,经验minZu = int(int(minZuan)/actionCount)maxZu = int(int(maxZuan)/actionCount)minGr = int(int(minGrow)/actionCount)maxGr = int(int(maxGrow)/actionCount)print u"钻石:", minZu, u"~", maxZuprint u"经验:", minGr, u"~", maxGroperationCourseData(key, courseData, minZu, maxZu, minGr, maxGr)#print(data)store(data)