请教 Python中coding指定编码方式,decode出错的问题...

kainazhang 2010-01-19 05:54:28
#!/usr/bin/python
#coding:utf-8

a="中文"
a.decode("utf8")

报告错误:
File "<stdin>", line 1, in ?
File "/usr/lib64/python2.4/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode bytes in position 0-1: invalid data

为什么执行上面的代码会出错呢?“#coding:utf-8”不是已经指定了其下面所有的字符的编码方式是utf-8吗?
...全文
5752 18 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
notax 2010-02-02
  • 打赏
  • 举报
回复
因为
[Quote=引用 10 楼 angel_su 的回复:]
应该同你用的编辑软件有关,使用python提供的idel编辑的话,能根据coding:xxx来编码储存文件,然而大多数编辑器才不会理会你的文本内容写啥,文件编码要在菜单某选项里设置好。
[/Quote]
kainazhang 2010-02-02
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 notax 的回复:]
in vim or .vimrc
=>>
set enc=utf8


或用gedit
save as utf8 character coding

试试看



[/Quote]
可以解释一下为什么这样就可以吗?
tcww88 2010-01-30
  • 打赏
  • 举报
回复
看一下你的locale~是zh_CN.utf-8 or en_US.utf-8吗
cealcomedown 2010-01-28
  • 打赏
  • 举报
回复
楼上正解
糊糊 2010-01-26
  • 打赏
  • 举报
回复
因为你的源码保存的时候是gbk的编码,所以会出错。加了#coding:utf-8 也没有用。
hercaffe 2010-01-26
  • 打赏
  • 举报
回复
lz 直接在你的源码第一行添加 #coding=gbk



============================

希望能帮到你 , 我空间的博客你也可以参考下
http://blog.csdn.net/hercaffe/archive/2010/01/26/5256314.aspx

ps。如果需要转载请注明出处 谢谢
kainazhang 2010-01-22
  • 打赏
  • 举报
回复
我用的是LINUX的vim~~
notax 2010-01-22
  • 打赏
  • 举报
回复
in vim or .vimrc
=>>
set enc=utf8


或用gedit
save as utf8 character coding

试试看


不得闲 2010-01-21
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 thy38 的回复:]
# -*- coding:utf-8 -*-
一般放在第一行
[/Quote]
用这个
angel_su 2010-01-21
  • 打赏
  • 举报
回复
应该同你用的编辑软件有关,使用python提供的idel编辑的话,能根据coding:xxx来编码储存文件,然而大多数编辑器才不会理会你的文本内容写啥,文件编码要在菜单某选项里设置好。不知道lz用啥编辑器?
kainazhang 2010-01-21
  • 打赏
  • 举报
回复
下面的代码是一个叫做test.py的文件


# -*- coding:utf8 -*-
#!/usr/bin/python
a="中文"
a.decode("utf8")


执行
python test.py

报告错误:
File "test.py", line 2
SyntaxError: 'utf8' codec can't decode bytes in position 0-1: invalid data

我不明白的是:“#-*- coding:utf-8 -*- ”已经指定了整个test.py的编码方式是utf8,为什么使用a.decode("utf8")的时候还会报错,而是用a.decode("gbk")就可以呢?
kainazhang 2010-01-21
  • 打赏
  • 举报
回复
下面的代码是一个叫做test.py的文件

#-*- coding:utf-8 -*-
#!/usr/bin/python
a="中文"
a.decode("utf8")

执行
python test.py
报告错误:
Traceback (most recent call last):
File "test.py", line 4, in ?
a.decode("utf8")
File "/usr/lib64/python2.4/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode bytes in position 0-1: invalid data

我不明白的是:“#-*- coding:utf-8 -*- ”已经指定了整个test.py的编码方式是utf8,为什么使用a.decode("utf8")的时候还会报错,而是用a.decode("gbk")就可以呢?


dichaofeng 2010-01-21
  • 打赏
  • 举报
回复
肯定源文件 的编码格式不是utf-8
thy38 2010-01-20
  • 打赏
  • 举报
回复
# -*- coding:utf-8 -*-
一般放在第一行
notax 2010-01-20
  • 打赏
  • 举报
回复
#coding:utf-8
or
#xxx coding=utf-8 xxxx

it doesn't matter


PEP: 0263

...
To define a source code encoding, a magic comment must
be placed into the source files either as first or second
line in the file, such as:

# coding=<encoding name>

or (using formats recognized by popular editors)

#!/usr/bin/python
# -*- coding: <encoding name> -*-
...


More precisely, the first or second line must match the regular
expression "coding[:=]\s*([-\w.]+)".
icosagon 2010-01-20
  • 打赏
  • 举报
回复
还要看你的源码文件是什么编码方式。
winbsd 2010-01-20
  • 打赏
  • 举报
回复
#coding:utf-8
#!/usr/bin/python

a="中文"
a.decode("utf8")
goosman 2010-01-19
  • 打赏
  • 举报
回复
# -*- coding:utf-8 -*-

我一般编码是这样指定的, 不知道是不是这个原因

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧