??????峤 发表于 2017-4-24 10:41:34

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
  这样程序就可以正常运行了。
  eg1:
  #!/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
  eg2:
  #!/usr/bi/python
#-*- coding:GBK -*-
  s1=input('Input your name:')
print('你好,%s' % s1)
页: [1]
查看完整版本: python解决中文编码问题