免费注册 查看新帖 |

Chinaunix

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

包含中文的代码的运行问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-03-18 21:55 |只看该作者 |倒序浏览
包含中文代码的脚本运行时只要把IDE中的设置调成UFT-8就行了吗?我怎么这样做大多数时候还是不行

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import hol
  File "D:\Python25\hol.py", line 42
SyntaxError: Non-ASCII character '\xd1' in file D:\Python25\hol.py on line 42, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details (hol.py, line 42)

调成UFT-8以后运行一个脚本还是这样报我错!

论坛徽章:
0
2 [报告]
发表于 2007-03-18 22:45 |只看该作者
原帖由 zxfyaye 于 2007-3-18 21:55 发表
包含中文代码的脚本运行时只要把IDE中的设置调成UFT-8就行了吗?我怎么这样做大多数时候还是不行

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
  ...


在中文前再加个u,例如:u'帮助'

论坛徽章:
0
3 [报告]
发表于 2007-03-19 08:59 |只看该作者
试试#-*-coding=cp936-*-

论坛徽章:
0
4 [报告]
发表于 2007-03-19 09:21 |只看该作者
上述两种办法试过,虽然不报错,但代码如下:
a = u'你好'
print a
u'\xc4\xe3\xba\xc3'

怎么样才能正常显示汉字呢?

论坛徽章:
0
5 [报告]
发表于 2007-03-19 10:05 |只看该作者
试试这个:

# -*- coding: utf_8 -*-
a = u'你好'
print a.encode('cp936')

论坛徽章:
0
6 [报告]
发表于 2007-03-19 10:25 |只看该作者
原帖由 zxfyaye 于 2007-3-19 09:21 发表
上述两种办法试过,虽然不报错,但代码如下:
a = u'你好'
print a
u'\xc4\xe3\xba\xc3'

怎么样才能正常显示汉字呢?


下面这样就可以了
# -*- coding: utf-8 -*-

print u'帮助'

论坛徽章:
0
7 [报告]
发表于 2007-03-20 02:16 |只看该作者
>>> #-*- coding:utf-8 -*-
>>> print u'帮助'
°&iuml;&Ouml;ú
>>> a = u'你好'
>>> a
u'\xc4\xe3\xba\xc3'
>>> # -*- coding:utf-8 -*-
>>> a = u'你好'
>>> print a.encode('cp936')

Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    print a.encode('cp936')
UnicodeEncodeError: 'gbk' codec can't encode character u'\xc4' in position 0: illegal multibyte sequence
>>> # -*- encoding:utf-8 -*-
>>> a = u'你好'
>>> a
u'\xc4\xe3\xba\xc3'
>>>

救命啊大虾们,怎么弄都是这样,到底要怎么解决这个问题啊,晕倒了!

论坛徽章:
0
8 [报告]
发表于 2007-03-20 12:19 |只看该作者
这个不是在命令行下运行的东西。

论坛徽章:
0
9 [报告]
发表于 2007-03-21 20:04 |只看该作者
我把上述的代码写进脚本,然后import,有以下报错:

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import wxtest
UnicodeDecodeError: 'utf8' codec can't decode bytes in position 0-1: invalid data

继续求教,该在哪里写代码,然后怎么运行,谢谢

论坛徽章:
0
10 [报告]
发表于 2007-03-22 15:20 |只看该作者
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP