您的位置:首页 > 编程语言 > Python开发

【再回首Python之美】【乱码】治疗输出中文却显示乱码的杂症

2018-02-08 21:44 309 查看
一排给csdn
解决方法1
  脚本首行加#coding:utf-8
  print u"我是中文字符串"
解决方法2
  str = "我是中文字符串"
 print str.decode("UTF-8")
解决方法3
  将方法2定义为一个函数,以后调用使用。def decode_str(sentence):
return sentence.decode("UTF-8")  print decode_str("我是中文字符串")

示例代码#coding:utf-8
#ex_decode_chinese.py
self_file = __file__ #save current file absolute path

#解决python脚本输出含有中文的字符串,而执行后却出现乱码的问题

print "\n===1st method==="
print u"你好,我治疗乱码。"

print "\n===2nd method==="
sentence = "你好,我也治疗乱码。"
print "%s" % sentence.decode("UTF-8")

print "\n===3rd method==="
def decode_str(sentence):
return sentence.decode("UTF-8")

print decode_str("你好,我也能治疗乱码。")

print "\nexit %s" % self_file


编译执行



(end)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: