python编码(汉字)

来源:互联网 发布:淘宝头像尺寸 编辑:程序博客网 时间:2024/06/03 20:00

运行.py程序时,如果里面含有汉字,就需要定义编码格式。

Python will default to ASCII as standard encoding if no other    encoding hints are given.

有以下几种方式(对win7x86+python2.7.10有效):

1. 在.py文件的第一行写下:

# This Python file uses the following encoding: utf-8

2. 在.py文件的第一行和第二行写下:

#!/usr/bin/python# -*- coding: utf-8 -*-

3. 在.py文件的前两行写下:

#!/usr/bin/python# vim: set fileencoding: utf-8

 

 

具体问题解决页面在https://www.python.org/dev/peps/pep-0263/

 

其中一种解决方法:

# coding = UTF-8

并不能解决问题,可能是系统或版本的问题。

0 0