首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

python解决中文编码有关问题

2012-09-01 
python解决中文编码问题程序中出现中文,运行的时候出现如下错误:?SyntaxError: Non-UTF-8 code starting w

python解决中文编码问题

程序中出现中文,运行的时候出现如下错误:

?

SyntaxError: Non-UTF-8 code starting with '\xb1' in file rate.py on line 5, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

?

导致出错的根源就是编码问题。

解决方案是:

???? 在程序最上面加上:# coding=gbk

这样程序就可以正常运行了。

?

eg:

?

#!/usr/bin/python
#-*- coding:GBK -*-

?

principal = 1000 # Initial amount (本金)
rate = 0.05 # Interest rate (利率)
numyears = 5 # Number of years (期数,年)
year = 1

?

while year <= numyears:
??????? principal = principal*(1+rate)
??????? print (year, principal)
??????? year += 1

热点排行