在Python 2中输出汉字,您需要确保文件编码设置为UTF-8,并且在使用`print`函数时,将字符串转换为Unicode。以下是两种常见的方法:
方法一:使用`encode`和`decode`
-*- coding: utf-8 -*-
print u'你好,世界'
方法二:设置默认编码
-*- coding: utf-8 -*-
reload(sys)
sys.setdefaultencoding('utf8')
print '你好,世界'
请注意,在Python 2中,您可能需要使用`u`前缀来表示Unicode字符串,并且在文件开头添加编码声明` -*- coding: utf-8 -*-`来告诉Python解释器使用UTF-8编码。