这个是编码问题啊 open()def readFile(fileName):
file = open(fileName,'r',encoding='utf-8')
res = file.read()
file.close()
return res
def countComments(result):
resList = result.split(',')
commentCnts = {}
for res in resList:
commentCnts[res] = commentCnts.get(res, 0) + 1
return commentCnts
result = readFile("C:\\py\\result.txt")
dicCnts = countComments(result)
print()
file = open("C:\\py\\result.txt",'a',encoding='utf-8')
most=max(dicCnts.values())
for k,v in dicCnts.items():
if v == most:
mostComment = k
break
file.write("\n根据统计,对今天伙食感觉:\n")
file.write("'很满意'的学生{}人;\n".format(dicCnts["很满意"]))
file.write("'满意'的学生{}人;\n".format(dicCnts["满意"]))
file.write("'一般'的学生{}人;\n".format(dicCnts["一般"]))
file.write("'不满意'的学生{}人。\n".format(dicCnts["不满意"]))
file.write("出现次数最多的评语是{}\n".format(mostComment))
file.close()
|