最新消息:20210917 已从crifan.com换到crifan.org

【已解决】Python脚本语法错误:SyntaxError: (unicode error) ‘utf8’ codec can’t decode byte 0xc0 in position 0: invalid start byte v2012-01-15

StringEncoding crifan 8722浏览 0评论

【已解决】Python脚本语法错误:SyntaxError: (unicode error) 'utf8' codec can't decode byte 0xc0 in position 0: invalid start byte
 

 

D:tmpWordPressOthersto_wphi-baidu-mover_v2>hi-baidu-mover_v2011-12-22-office.py -f http://hi.baidu.com/recommend_music/blog/item/de233143bd84211a72f05deb.html -l 1
File "D:tmpWordPressOthersto_wphi-baidu-mover_v2hi-baidu-mover_v2011-12-22-office.py", line 869
cat_no_unicode = opt_no_unicode.replace(u'类别:', '')
SyntaxError: (unicode error) 'utf8' codec can't decode byte 0xc0 in position 0: invalid start byte

【背景】

一个python脚本,运行的时候出错了:

 

【解决过程】

1.觉得很奇怪的一点是,这个脚本,一直是可以正常运行的,怎么突然就出现了语法错误了。

然后发现是带有中文的代码,会出现错误。

第一反应是,可能是安装了那个python的Python Imaging Library (PIL)库,然后影响了当前的python 2.7.2版本的语法了,影响成为类似于python 3.x的语法了。

因为之前试过,python 3.x版本中,对于上述写法,即u加上中文,比如:

u‘这是中文’,是不支持的。而python 2.x的语法是支持的,表示这个字符是unicode的字符,会自动处理编码,成为unicode变量的。

所以,就是尝试了,卸载掉之前安装的PIL-1.1.7.win32-py2.7.exe,结果问题依旧。

2.对应着当前这个python脚本,之前还有一个版本,里面也有同样的这部分代码,结果却是,那个脚本可以正常执行,这个脚本无法执行,出现这个SyntaxError: (unicode error)错误,所以更加异怪了。

本来想找个beyondcompare来比较两个脚本之前到底有哪些区别的,结果由于暂时不方便下载安装使用beyondcompare,只好作罢。

3.重新安装了python 2.7.2,问题依旧。

4.重启电脑,问题依旧。

5.后来,把上述代码中中文字符前缀的u,改为函数unicode加上中文字符,即:

 

cat_no_unicode = opt_no_unicode.replace(unicode('类别:'), '')

 

 结果,就可以正常执行了,没有了这个语法错误。

6.然后才突然想到,是不是脚本文件本身的编码不匹配,一去查看当前所用的notepad++中的当前脚本文件所用的编码,果然,用的是默认的ANSI,而不是utf-8的编码,所以,u"类别:",无法识别,而 去notepad+中,格式->转为utf-8编码,后,再保存文件,再去运行脚本,就可以支持u"类别:"了。

当然,对应的unicode(“类别:”)这样的做法,起始是最安全的,其不会受到你当前python脚本文件所用编码的影响。

 

【总结】

1。如果python中所要处理的字符串中包含中文,那么最好要搞懂所用字符的编码,是gbk/gb2312/gb18030,还是utf-8,否则容易出现乱码,以及此处的语法错误。

而为了保险起见,最好用unicode("中文字符")的方式,来使用,操作这些中文字符。

当然,如果你自己需要,自己也知道,那最好unicode("中文字符").encode("utf-8")的方法,把其转为utf-8的格式,这个编码最通用。

2.notepad++新建的文件,也最好使用比较通用的utf-8的格式来存储文件,而不要用默认的ANSI,否则其中的中文,会由于ANSI不支持,而默认用你本地语言,比如我此处的GBK,去编码,这样容易出现一些编码类的错误。

 



【后记1】

本来觉得上面的理解是完全正确的。

结果证实了,上面的理解,是错误的。。。

实际的例子是:

如果用unicode("下午").encode("utf-8"),然后python脚本可以执行通过,但是结果却不对,无法和我从网页中抓取到的utf-8的“下午“相匹配,导致代码运行结果不是我们想要的。

而只有通过u”下午“.encode("utf-8")得到的结果,才和我网页中抓取到的utf-8的”下午“相等,代码才可以按照预期的结果去执行。

所以,结论是:

【总结】

1.python脚本文件所用编码,如果可以,最好用utf-8.

2.脚本中,用到的中文字符,具体是unicode(”中文“).encode("utf-8"),还是u”中文”.encode("utf-8"),你要自己尝试,才知道结果如何。至少我这里的,是后者,程序执行结果才是对的。

 



【后记2】

1.经过测试:

此处我从网页抓取的中文字符moring_afternoon_zhCN,isinstance(moring_afternoon_zhCN, unicode)的结果,是False,即不是unicode字符。和之前介绍的,网页内容被beautifulsoup处理后,就自动转为utf-8这点,所不一致了。暂未搞懂是啥原因。

而且实际一个是utf-8的字符和这个字符比较:
 

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; afternoon_zhCN_utf8 = u"下午".encode("utf-8")<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #if unicode(moring_afternoon_zhCN).encode("utf-8") == afternoon_zhCN_utf8:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if unicode(moring_afternoon_zhCN).encode("utf-8") == unicode("下午").encode("utf-8") :&nbsp; # this line can not excute!!!<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hour = str(int(hour) + 8) </p>


结果也会出错的:

&lt;table border=&quot;1&quot; cellspacing=&quot;1&quot; cellpadding=&quot;1&quot; width=&quot;80%&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&amp;nbsp;&amp;nbsp; File &quot;D:tmpWordPressOthersto_wphi-baidu-mover_v2hi-baidu-mover_v2011-12-22-office.py&quot;, line 1169, in parseAndSetEntryDatetime&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if unicode(moring_afternoon_zhCN).encode(&quot;utf-8&quot;) == unicode(&quot;涓嬪崍&quot;).encode(&quot;utf-8&quot;) :&amp;nbsp; # this line can excute, but result is worng !!!&lt;br /&gt;UnicodeDecodeError: &#39;ascii&#39; codec can&#39;t decode byte 0xe4 in position 0: ordinal not in range(128)&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;

2. 上面的isinstance(u"下午".encode("utf-8"), unicode)结果为False,即不是unicode字符,这点需要注意一下的。因为本身u"下午"是unicode字符,属于unicode类型,被转换编码encode("utf-8")后,就是普通的字符,属于str类型了。

【后记 2012-01】
前面说到的

此处我从网页抓取的中文字符moring_afternoon_zhCN,isinstance(moring_afternoon_zhCN, unicode)的结果,是False,即不是unicode字符。和之前介绍的,网页内容被beautifulsoup处理后,就自动转为utf-8这点,所不一致了。

的理解,是不对的。实际情况是,beautifulsoup处理后的网页,的确是utf-8的。而utf-8和unicode,本身就不相等。
首先,字符串分两种,一种是str字符,一种是unicode字符。str字符,就是普通字符,某种编码的字符,包括ANSI,GBK,UTF-8等,而Unicode就是原始的unicode,未经过encode编码的unicode,而进过某种编码,比如utf-8,gbk等编码后的unicode,就是对应str类型了。
 

转载请注明:在路上 » 【已解决】Python脚本语法错误:SyntaxError: (unicode error) ‘utf8’ codec can’t decode byte 0xc0 in position 0: invalid start byte v2012-01-15

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
93 queries in 0.199 seconds, using 23.41MB memory