欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python編碼爬坑指南(必看)

 更新時(shí)間:2016年06月10日 11:45:11   投稿:jingxian  
下面小編就為大家?guī)硪黄狿ython編碼爬坑指南(必看)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

自己最近有在學(xué)習(xí)python,這實(shí)在是一門非常短小精悍的語言,很喜歡這種語言精悍背后又有強(qiáng)大函數(shù)庫(kù)支撐的語言??墒莿偨佑|不久就遇到了讓人頭疼的關(guān)于編碼的問題,在網(wǎng)上查了很多資料現(xiàn)在在這里做一番總結(jié),權(quán)當(dāng)一個(gè)記錄也為后來的兄弟姐妹們服務(wù),如果可以讓您少走一些彎路本人將倍感榮幸。

先來描述下現(xiàn)象吧:

import os
for i in os.listdir("E:\Torchlight II"):
  print i

代碼很簡(jiǎn)單我們使用os的listdir函數(shù)遍歷了E:\Torchlight II這個(gè)目錄(Torchlight ?! :)),由于這個(gè)目錄下有些文件是以中文命名的,所以在最后print結(jié)果時(shí)出現(xiàn)了亂碼,像這樣:

那么問題出在哪兒呢? 別急,我們一點(diǎn)一點(diǎn)來分析它。

這里這里我們幾乎能夠肯定的知道問題是出在:

This means that the python console app can't write the given character to the console's encoding.
More specifically, the python console app created a _io.TextIOWrapperd instance with an encoding that cannot represent the given character.
sys.stdout --> _io.TextIOWrapperd --> (your console)

看到這里不知你是否與我想的一樣,能不能去設(shè)置console的編碼,將其設(shè)置為能夠理解中文字符的編碼不就可以正常的顯示出中文了嗎?等等,讓我們?cè)诙郍oogle一會(huì)兒,

Python determines the encoding of stdout and stderr based on the value of the LC_CTYPE variable, but only if the stdout is a tty. So if I just output to the terminal, LC_CTYPE (or LC_ALL) define the encoding. However, when the output is piped to a file or to a different process, the encoding is not defined, and defaults to 7-bit ASCII.

更詳細(xì)的說明如下:

1). When Python finds its output attached to a terminal, it sets the sys.stdout.encoding attribute to the terminal's encoding. The print statement's handler will automatically encode unicode arguments into str output.
2). When Python does not detect the desired character set of the output, it sets sys.stdout.encoding to None, and print will invoke the "ascii" codec.

嚯嚯,看來剛才的想法是可行的只是不太優(yōu)雅罷了,因?yàn)槲覀兊萌バ薷南到y(tǒng)的設(shè)置。事實(shí)上上面的論述是基于linux環(huán)境的,在linux下可能需要我們?nèi)ジ哪硞€(gè)環(huán)境變量的值(LC_CTYPE or LANG);如果我們是在windows下面的話,console的編碼設(shè)置是跟操作系統(tǒng)的區(qū)域設(shè)置相關(guān)的。比如在中文的win7環(huán)境下,console默認(rèn)的編碼就是GBK(cp936)。你可以試試下面的代碼:

import locale
print locale.getdefaultlocale()[1]

console的編碼不好設(shè)置了那能否對(duì)stdout.out.encoding進(jìn)行設(shè)置以達(dá)到我們的目的呢?很遺憾,答案是否定的,這家伙壓根就是只讀的:

沒有辦法了么?不會(huì),其實(shí)我們離成功已經(jīng)很近了,來,根據(jù)上面檢索到的那些資料分析整理下看看我們現(xiàn)在掌握到的情況都有哪些:

 

1). console不能正常顯示中文,console的編碼是由操作系統(tǒng)決定的(windows環(huán)境下);
 2). 我的操作系統(tǒng)是win7中文版(GBK),enc = locale.getdefaultlocale()[1];
 3). console的編碼決定了sys.stdout.encoding的取值,sys.stdout.encoding = utf-8;
 4). 從操作系統(tǒng)枚舉目錄(E:\Torchlight II)列表返回的字符串也是GBK編碼

 是不是已經(jīng)看出問題來了。最上面截圖中那么奇奇怪怪的問號(hào)尖角符號(hào)就是因?yàn)樽址旧硎前凑誫bk進(jìn)行編碼的,但是由于sys.stdout.encoding = utf-8,導(dǎo)致print會(huì)按照utf-8對(duì)input的數(shù)據(jù)進(jìn)行encode從而轉(zhuǎn)換為unicode字符。這,當(dāng)然錯(cuò)誤了。原因已經(jīng)清楚了,來改改代碼吧:

import os
for i in os.listdir("E:\Torchlight II"):
  print i.decode('gbk')

在代碼中我們手動(dòng)告訴了python對(duì)讀入的字符串按章gbk編碼來進(jìn)行解碼,而這一個(gè)動(dòng)作之后數(shù)據(jù)已經(jīng)是標(biāo)準(zhǔn)的unicode字符了,可以放心的交給print去打印輸出了(即使這會(huì)兒sys.stdout.encoding = utf-8):

 ps:

實(shí)際在google中還查到過很多相關(guān)的類似編碼的問題,比如這里的,還有這里的。雖然問題的樣子千變?nèi)f化并且解決方式多種多樣甚至是python自己的特定解決方式,比如這里。但這些問題本質(zhì)都是一樣的都是關(guān)于字符的編碼和解碼,搞清楚了其中的本質(zhì)所有問題都能夠迎刃而解。

以上這篇Python編碼爬坑指南(必看)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論