免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6397 | 回复: 7
打印 上一主题 下一主题

SyntaxError: Non-ASCII character '\xc4' [复制链接]

论坛徽章:
3
天秤座
日期:2014-10-29 11:37:572015元宵节徽章
日期:2015-03-06 15:50:39NBA常规赛纪念章
日期:2015-05-04 22:32:03
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-10-28 13:51 |只看该作者 |倒序浏览
本帖最后由 tomer 于 2014-10-28 13:56 编辑

  1. [root@VM test]# more t1
  2. #!/usr/bin/python
  3. s1=input("input you name:")
  4. print("你好,%s" % s1)

  5. [root@VM test]# ./t1
  6.   File "./t1", line 3
  7. SyntaxError: Non-ASCII character '\xc4' in file ./t1 on line 3, but no encoding declared; see [url]http://www.python.org/peps/pep-0263.html[/url] for details
复制代码

论坛徽章:
3
天秤座
日期:2014-10-29 11:37:572015元宵节徽章
日期:2015-03-06 15:50:39NBA常规赛纪念章
日期:2015-05-04 22:32:03
2 [报告]
发表于 2014-10-28 14:02 |只看该作者
本帖最后由 tomer 于 2014-10-28 14:03 编辑

明白了,如果有中文,前面要加上
#coding=utf-8
不过执行:

  1. [root@VM test]# ./t1
  2. input you name:
复制代码
为什么输入字符串需要加""
如果不用引号括,就报错

  1. [root@VM test]# ./t1
  2. input you name:name
  3.   File "./t1", line 3, in <module>
  4.     s1=input("input you name:")
  5.   File "<string>", line 1, in <module>
  6. NameError: name 'name' is not defined
复制代码

论坛徽章:
0
3 [报告]
发表于 2014-10-28 15:04 |只看该作者
本帖最后由 whitelotus19 于 2014-10-28 15:05 编辑

不知道是不是这种问题?
http://bbs.chinaunix.net/thread-4157802-1-1.html

你用的python2.x版本?

论坛徽章:
3
天秤座
日期:2014-10-29 11:37:572015元宵节徽章
日期:2015-03-06 15:50:39NBA常规赛纪念章
日期:2015-05-04 22:32:03
4 [报告]
发表于 2014-10-28 15:09 |只看该作者
回复 3# whitelotus19


    是的,我的版本Python 2.6.6

论坛徽章:
0
5 [报告]
发表于 2014-10-28 15:39 |只看该作者
回复 4# tomer


python2.x用raw_input()的吧,
打印输出用print语句的,不是用print()函数   

论坛徽章:
3
天秤座
日期:2014-10-29 11:37:572015元宵节徽章
日期:2015-03-06 15:50:39NBA常规赛纪念章
日期:2015-05-04 22:32:03
6 [报告]
发表于 2014-10-29 09:47 |只看该作者
回复 5# whitelotus19

今天装了3.4.1版:

  1. [root@VM test]# more t1
  2. #!/usr/bin/python
  3. #coding=utf-8
  4. s1=input("input you name:")
  5. print("你好,%s" % s1)


  6. [root@VM test]#./t1
  7.   File "./t1", line 4
  8. SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xc4 in position 0: invalid continuation byte

复制代码

论坛徽章:
0
7 [报告]
发表于 2014-10-29 12:20 |只看该作者
tomer 发表于 2014-10-29 09:47
回复 5# whitelotus19

今天装了3.4.1版:


我刚装了个3.4.2的,好像没有这样子的报错。
  1. [root@centos7pc1 temp]# cat t1.py
  2. #!/python34/python
  3. #coding:utf-8

  4. import sys

  5. print('version:',sys.version,sys.platform)
  6. s1=input('input your name:')
  7. print('你好,%s' % s1)

  8. [root@centos7pc1 temp]# ./t1.py
  9. version: 3.4.2 (default, Oct 29 2014, 12:07:47)
  10. [GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] linux
  11. input your name:lotus
  12. 你好,lotus
  13. [root@centos7pc1 temp]# echo $LANG
  14. en_US.UTF-8
  15. [root@centos7pc1 temp]#
复制代码

论坛徽章:
3
天秤座
日期:2014-10-29 11:37:572015元宵节徽章
日期:2015-03-06 15:50:39NBA常规赛纪念章
日期:2015-05-04 22:32:03
8 [报告]
发表于 2014-10-29 12:30 |只看该作者
回复 7# whitelotus19


    恩,我把系统的LANG改成en_US.UTF-8
    也可以了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP