一排给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)