怎么用python讀取utf-8編碼格式的文本文件-創(chuàng)新互聯(lián)

本篇內(nèi)容介紹了“怎么用python讀取utf-8編碼格式的文本文件”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

成都創(chuàng)新互聯(lián)10多年成都定制網(wǎng)站服務(wù);為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁(yè)設(shè)計(jì)及高端網(wǎng)站定制服務(wù),成都定制網(wǎng)站及推廣,對(duì)成都石雕等多個(gè)方面擁有豐富的網(wǎng)站推廣經(jīng)驗(yàn)的網(wǎng)站建設(shè)公司。
# 讀取utf-8編碼格式的文本文件 
# 這里Python解釋器讀取的是utf-8編碼的字節(jié)流,然后再按指定的編碼方式解釋這些字節(jié)流
# 這樣就比較好理解亂碼的原因


#coding=utf-8   
# When Python reads the encoding it tries to interpret the file as utf-8 
# 告訴Python解釋器編譯時(shí)采用哪種編碼方式
# 如未設(shè)置編碼方式,且解釋器可識(shí)別文件(如utf-8編碼格式的文件有的(與編輯器有關(guān))帶有BOM,可供解釋器識(shí)別)編碼方式則采用文件編碼方式,反之采用終端默認(rèn)編碼方式


import sys
# reload(sys)
# 指定終端默認(rèn)編碼方式
# sys.setdefaultencoding('utf8')
# 獲取終端默認(rèn)編碼方式
# sys.getdefaultencoding()


import codecs
def ConvertCN(s):
    if s[:3] == codecs.BOM_UTF8:
        s = s[3:]
        
    # 若系統(tǒng)默認(rèn)編碼方式為acsii且未修改默認(rèn)編碼方式 
    # s.encode('gbk'):s(文件編碼格式)--(.decode('ascii'))-->unicode--(.encode('gbk'))-->s(gbk編碼格式) (這個(gè)過(guò)程由終端完成,未指定編碼方式時(shí)將采用終端默認(rèn)編碼方式)
    # 兼容關(guān)系:ASCII --> ISO-8859-1 --> UNICODE(UTF-8,UTF-16,UTF-32) --> UCS(UCS2[與UNICODE兼容],UCS4), ASCII --> GB2312 --> GBK --> GB18030
    # 因utf-8向下兼容ascii ascii不向上兼容utf-8,對(duì)此處s(utf-8編碼格式)進(jìn)行ascii解碼成unicode時(shí)可能發(fā)生錯(cuò)誤
    return s.decode('utf-8')


def PrintFile(filename):
    f = file(filename,'r')
    for f_line in f.readlines():
        print ConvertCN(f_line)
    f.close()
    
if __name__ == "__main__":
    
    PrintFile('OperCodingFile.txt')
    
    # Python輸出的是字節(jié)流 打印由終端處理
    # print 在終端顯示如何是由終端決定的
    """
    它大致講解下python中的print原理: 
    When Python executes a print statement, 
    it simply passes the output to the operating system (using fwrite() or something like it), 
    and some other program is responsible for actually displaying that output on the screen. 
    For example, on Windows, it might be the Windows console subsystem that displays the result. 
    Or if you're using Windows and running Python on a Unix box somewhere else, 
    your Windows SSH client is actually responsible for displaying the data. 
    If you are running Python in an xterm on Unix, then xterm and your X server handle the display. 
    To print data reliably, you must know the encoding that this display program expects. 
    簡(jiǎn)單地說(shuō),python中的print直接把字符串傳遞給操作系統(tǒng),所以你需要把str解碼成與操作系統(tǒng)一致的格式。
    Windows使用CP936(幾乎與gbk相同),所以這里可以使用gbk。
    """
    
    print ConvertCN("\n****** 按任意鍵退出!*******")  
    
    sys.stdin.readline()

“怎么用python讀取utf-8編碼格式的文本文件”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

網(wǎng)站名稱:怎么用python讀取utf-8編碼格式的文本文件-創(chuàng)新互聯(lián)
文章網(wǎng)址:http://muchs.cn/article20/eipjo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、網(wǎng)站改版、企業(yè)建站、移動(dòng)網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站、營(yíng)銷型網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)