<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Townes &#187; 中文decode和encode</title>
	<atom:link href="https://thetownes.coolpage.biz/?feed=rss2&#038;tag=%E4%B8%AD%E6%96%87decode%E5%92%8Cencode" rel="self" type="application/rss+xml" />
	<link>https://thetownes.coolpage.biz</link>
	<description>本站已转移至：http://www.thetownes.info</description>
	<lastBuildDate>Thu, 01 Aug 2013 11:49:56 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.2</generator>
		<item>
		<title>python中文decode和encode转码</title>
		<link>https://thetownes.coolpage.biz/?p=482</link>
		<comments>https://thetownes.coolpage.biz/?p=482#comments</comments>
		<pubDate>Thu, 01 Aug 2013 11:48:47 +0000</pubDate>
		<dc:creator>Will</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[中文decode和encode]]></category>

		<guid isPermaLink="false">http://thetownes.coolpage.biz/?p=482</guid>
		<description><![CDATA[字符串在Python内部的表示是unicode编码，因此，在做编码转换时，通常需 &#8230; <a href="https://thetownes.coolpage.biz/?p=482">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>字符串在Python内部的表示是unicode编码，因此，在做编码转换时，通常需要以unicode作为中间编码，即先将其他编码的字符串解码（decode）成unicode，再从unicode编码（encode）成另一种编码。</p>
<p>decode的作用是将其他编码的字符串转换成unicode编码，如str1.decode(&#8216;gb2312&#8242;)，表示将gb2312编码的字符串str1转换成unicode编码。</p>
<p>encode的作用是将unicode编码转换成其他编码的字符串，如str2.encode(&#8216;gb2312&#8242;)，表示将unicode编码的字符串str2转换成gb2312编码。</p>
<p>因此，转码的时候一定要先搞明白，字符串str是什么编码，然后decode成unicode，然后再encode成其他编码</p>
<p>（与代码本身的编码是一致的！）</p>
<p>测试：<br />
我的eclipse里面代码为utf-8编码的。然后我这样写代码<br />
s=&#8221;你好&#8221;<br />
s=s.decode(&#8216;gb2312&#8242;).encode(&#8216;utf-8&#8242;)<br />
print s<br />
报错：<br />
UnicodeDecodeError: &#8216;gb2312&#8242; codec can&#8217;t decode bytes in position 2-3: illegal multibyte sequence<br />
原因：因为我的文件为UTF-8编码的。所以你想用gb2312将其转成unicode是不可能的。<br />
所以正确的写法应当是：<br />
s=&#8221;你好&#8221;<br />
print s<br />
s=s.decode(&#8216;utf-8&#8242;).encode(&#8216;utf-8&#8242;) 要用UTF-8来做编码<br />
print s<br />
哈哈发现打印出来的是乱码那只能说明一件事情就是我的eclipse控制台是GB2312的编码！</p>
<p>请看：<br />
如何获得系统的默认编码？<br />
#!/usr/bin/env python<br />
#coding=utf-8<br />
import sys<br />
print sys.getdefaultencoding()</p>
<p>该段程序在英文WindowsXP上输出为：ascii 。我发现我的linux上面也是ascii编码。所以我想打印出来看到的乱码是正常的。因为我其实是utf-8编码的。</p>
<p>在某些IDE中，字符串的输出总是出现乱码，甚至错误，其实是由于IDE的结果输出控制台自身不能显示字符串的编码，而不是程序本身的问题。（是的。我的eclipse控制台就是gb2312的编码所以我文件保存为utf-8的时候然后再通过打印是乱码了！）<br />
1、读文件命令肯定是：<br />
myfile = codecs.open(&#8220;c.<a href="http://www.2cto.com/kf/qianduan/css/" target="_blank">html</a>&#8220;,&#8221;r&#8221;,&#8221;utf-8&#8243;) 因为我用gb2312来读的话报错</p>
<p>心得：检查一个字符串是什么编码只需要看一下decode 如果用gb2312来decode没报错的话就表示是gb2312<br />
如果用utf-8来decode没有报错的话就表示是utf-8</p>
<p>现在遇到一个问题就是<br />
请看：<br />
myfile = codecs.open(&#8220;c.html&#8221;,&#8221;r&#8221;,&#8221;utf-8&#8243;)<br />
str = myfile.read()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
content = str.replace(&#8220;\n&#8221;,&#8221; &#8220;)&nbsp;&nbsp;<br />
content = content.encode(&#8216;utf-8&#8242;)<br />
print content<br />
没有报错<br />
再看：<br />
myfile = codecs.open(&#8220;c.html&#8221;,&#8221;r&#8221;,&#8221;utf-8&#8243;)<br />
str = myfile.read()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #显示中文<br />
content = str.replace(&#8220;\n&#8221;,&#8221; &#8220;)&nbsp;&nbsp;<br />
content = content.encode(&#8216;gb2312&#8242;)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 用gb2312<br />
print content<br />
报错：UnicodeEncodeError: &#8216;gb2312&#8242; codec can&#8217;t encode character u&#8217;\u2014&#8242; in position 12628</p>
<p>再看：<br />
myfile = codecs.open(&#8220;d.html&#8221;,&#8221;r&#8221;,&#8221;utf-8&#8243;)<br />
str = myfile.read()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #显示中文<br />
content = str.replace(&#8220;\n&#8221;,&#8221; &#8220;)&nbsp;&nbsp;<br />
content = content.encode(&#8216;gb2312&#8242;)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 用gb2312<br />
print content<br />
没问题<br />
myfile = codecs.open(&#8220;d.html&#8221;,&#8221;r&#8221;,&#8221;utf-8&#8243;)<br />
str = myfile.read()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #显示中文<br />
content = str.replace(&#8220;\n&#8221;,&#8221; &#8220;)&nbsp;&nbsp;<br />
content = content.encode(&#8216;utf-8&#8242;)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
print content<br />
也没问题</p>
<p>结论：我想是c.html页面里面 存在某些 特殊字符 只支持utf-8编码。而不支持gb2312的编码！<br />
而d.html没有这种特殊字符。这也就解释了为什么<br />
有的文件并没有发生我们想像中的问题！</p>
<p>所以我感觉打开文件肯定是用utf-8来读取得到一个unicode编码值！<br />
然后对其做utf-8的编码处理。因为如果你做gb2312处理的话就会报错了！</p>
<p>接着：<br />
我看了一下我的正则表达式发现如果用gb2312做解码处理的话一样会报错。所以断定肯定是utf-8编码了！<br />
regex3 = regex3.decode(&#8216;utf-8&#8242;)<br />
print type(regex3)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #返回为unicode码了！<br />
print regex3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #居然打印为正常的中文显示了 奇怪</p>
<p>尝试解决办法：<br />
1、全部用unicode处理<br />
即正则我用regex3 = regex3.decode(&#8216;utf-8&#8242;) 将其处理成 unicode编码了。然后内容也<br />
print type(content) 也是unicode编码。结果还是不行！</p>
<p>难道是我的linux终端的编码引起的吗？我看了一下<br />
locale 发现是GBK的终端的。即只有GBK编码才能显示出来为中文的！<br />
于是我将<br />
regex3 = regex3.decode(&#8216;utf-8&#8242;).encode(&#8216;gb2312&#8242;) 编码成gb2312结果可以显示中文！</p>
<p>OK。我又将我的内容也一起弄成GB2312<br />
content = content.encode(&#8216;gb2312&#8242;,&#8217;ignore&#8217;)<br />
print content&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 也可以成功打印出来中文。</p>
<p>我想这个时候应该没有什么问题了吧。结果一用正则又死掉了。昏死！！！！！！！</p>
<p>换另外一个好的文件测试下看看：换了之后发现没死而且成功了！</p>
<p>所以我觉得：肯定是这个文件里面的某些内容与正则匹配出现了冲突！导致的!</p>
<p>继续跟踪：<br />
出现如下的情况<br />
myfile = codecs.open(&#8220;01.htm&#8221;,&#8221;r&#8221;,&#8221;utf-8&#8243;,&#8221;ignore&#8221;)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
str = myfile.read()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
content = str.replace(&#8220;\n&#8221;,&#8221; &#8220;)<br />
print type(content)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #发现是unicode码<br />
regex3 = &#8216;class=wpcps<a href="http://www.2cto.com/kf/qianduan/css/" target="_blank">CSS</a>&gt;([^&lt;]+)(?:.*?wpcppb_CSS&gt; ([0-9]+) &lt;/span&gt;)?.*?(?:.*?(已被关闭))?.*?([0-9]+)个回答.*?([0-9]+)次浏览.*?(?:&lt;div&gt;.*?user\?userid=([0-9]+).*?&gt;(.*?)&lt;/a&gt; &lt;/div&gt;.*?)?(?:user\?userid=([0-9]+)&#8221;)?]+&#8221;&gt;([^&lt;]+).*?class=wpcptsCSS&gt;([^&lt;]+).*?([0-9.]{9,}\*).*?class=wpcpdCSS&gt;(.*?)&lt;/div&gt; &lt;div&gt;&#8217;<br />
content = content.encode(&#8216;utf-8&#8242;)<br />
p=re.compile(regex3)<br />
results = p.findall(content)</p>
<p>没有什么问题可以成功出来结果。但是我<br />
将content = content.encode(&#8216;gb2312&#8242;) 的话就发现 死掉了！<br />
说明我的内容content与我的正则的编码其实是不一样的！<br />
我现在将我的正则也调成gb2312来测试。结果发现可以出来。而且我的结果<br />
results = p.findall(content)<br />
for ele in results:<br />
&nbsp;&nbsp;&nbsp; print ele[0],ele[1],ele[2],ele[3],ele[4],ele[5],ele[6],ele[7],ele[8],ele[9],ele[10]<br />
在eclipse(默认为gb2312)下面也是没有问题的了！～</p>
<p>所以我想：如果content是GBK那正则的内容也应当是GBK 即两者的编码一定要保持一致！否则就会出现死掉程序的情况！</p>
<p>现在我这样来处理<br />
全部使用unicode编码处理</p>
<p>myfile = codecs.open(&#8220;right.html&#8221;,&#8221;r&#8221;)&nbsp;&nbsp;&nbsp;<br />
str = myfile.read()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
content = str.replace(&#8220;\n&#8221;,&#8221; &#8220;)<br />
content = content.decode(&#8216;utf-8&#8242;,&#8217;ignore&#8217;)&nbsp;&nbsp;&nbsp;&nbsp; #使用utf-8解码出来<br />
都使用unicode编码吧<br />
现在正则也用<br />
regex3 = regex3.decode(&#8216;utf-8&#8242;,&#8217;ignore&#8217;) 使用utf-8搞成unicode编码</p>
<p>OK现在再来测试！</p>
<p>结论：<br />
解决正则出现中文的BUG结论：<br />
1、打开文件<br />
myfile = codecs.open(&#8220;right.html&#8221;,&#8221;r&#8221;)<br />
不需要设置其编码的！</p>
<p>设置编码格式<br />
str = myfile.read()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
content = str.replace(&#8220;\n&#8221;,&#8221; &#8220;)<br />
content = content.decode(&#8216;utf-8&#8242;,&#8217;ignore&#8217;)&nbsp;&nbsp; #使用utf-8解码成unicode格式</p>
<p>正则：<br />
regex3 = regex3.decode(&#8216;utf-8&#8242;,&#8217;ignore&#8217;)&nbsp;&nbsp;&nbsp; #正则也统一使用utf-8解码成unicode格式</p>
<p>然后就可以<br />
p=re.compile(regex3)<br />
results = p.findall(content)<br />
调用正则了！</p>
]]></content:encoded>
			<wfw:commentRss>https://thetownes.coolpage.biz/?feed=rss2&#038;p=482</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
