Python学习笔记——解决中文输出时乱码

来源:互联网 发布:a链接调用js方法 编辑:程序博客网 时间:2024/06/06 01:30

今天学习Python时碰到,输出中文时,同一个程序,有的中文不乱码,有的乱码。 好像是各种编码之间的问题,不过最好的解决办法是在需要输出中文的地方用下面的格式

print u'中文。。。'

就是在那个引号前面加个 u ,具体可以看下面的示例

#score sort: Exp2_2.py#coding = UTF-8studscore = {u"唐僧": 45, u"孙悟空": 78, u"猪八戒": 40, u"沙僧": 96, u"如来": 65, u"观音": 90,             "白骨精": 78, "红孩儿": 99, "太上老君": 60, "白龙马": 87}maxscore = 0maxstudname = ''minscore = 100minstudname = ''avrscore = 0studnum = len (studscore)#输出所有成绩print u"成绩分别为:"for key in studscore.keys():    print key, studscore[key], ";",#换行print#进行成绩统计for key in studscore.keys():    if studscore[key] > maxscore:        maxscore = studscore[key]        maxstudname = key    if studscore[key] < minscore:        minscore = studscore[key]        minstudname = key    avrscore = avrscore + studscore[key]avrscore = avrscore / studnumprint u"全班共有", studnum , u"人, 平均成绩为:", avrscore, u"分。"print u"最高分是:", maxstudname, maxscore, u"分"print u"最低分是:", minstudname, minscore, u"分"

0 0
原创粉丝点击