SyntaxError: Non-ASCII character '\xc4'
本帖最后由 tomer 于 2014-10-28 13:56 编辑# more t1
#!/usr/bin/python
s1=input("input you name:")
print("你好,%s" % s1)
# ./t1
File "./t1", line 3
SyntaxError: Non-ASCII character '\xc4' in file ./t1 on line 3, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
本帖最后由 tomer 于 2014-10-28 14:03 编辑
明白了,如果有中文,前面要加上
#coding=utf-8
不过执行:
# ./t1
input you name:
为什么输入字符串需要加""
如果不用引号括,就报错
# ./t1
input you name:name
File "./t1", line 3, in <module>
s1=input("input you name:")
File "<string>", line 1, in <module>
NameError: name 'name' is not defined
本帖最后由 whitelotus19 于 2014-10-28 15:05 编辑
不知道是不是这种问题?
http://bbs.chinaunix.net/thread-4157802-1-1.html
你用的python2.x版本? 回复 3# whitelotus19
是的,我的版本Python 2.6.6 回复 4# tomer
python2.x用raw_input()的吧,
打印输出用print语句的,不是用print()函数 回复 5# whitelotus19
今天装了3.4.1版:
# more t1
#!/usr/bin/python
#coding=utf-8
s1=input("input you name:")
print("你好,%s" % s1)
#./t1
File "./t1", line 4
SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xc4 in position 0: invalid continuation byte
tomer 发表于 2014-10-29 09:47 static/image/common/back.gif
回复 5# whitelotus19
今天装了3.4.1版:
我刚装了个3.4.2的,好像没有这样子的报错。# cat t1.py
#!/python34/python
#coding:utf-8
import sys
print('version:',sys.version,sys.platform)
s1=input('input your name:')
print('你好,%s' % s1)
# ./t1.py
version: 3.4.2 (default, Oct 29 2014, 12:07:47)
linux
input your name:lotus
你好,lotus
# echo $LANG
en_US.UTF-8
# 回复 7# whitelotus19
恩,我把系统的LANG改成en_US.UTF-8
也可以了。
页:
[1]