Python處理中文語言 讀取中文

2021-07-22 10:26:10 字數 3759 閱讀 1169

本文解決問題:

1、匯入中文txt文字,並轉換為unicode

2、匯入包含中文的py file

-----------------------------------

解決問題一:

匯入中文txt文字,並轉換為unicode

-----------------------------------

1、unicode、utf-8

簡單理解,unicode是

一種處理所有非英文語言的編碼方式,即將每個語言中的每個文字設定成不同的數字,避免造成混亂。unicode目前覆蓋了世界上的主流語言,有超過一百多萬個編號。

utf-8是實現unicode的一種方式。

ascii是不同於unicode的另外一種編碼方式。詳細介紹可見參考文章1。

2、encoding

將文字轉化為數字的一系列規則叫作encoding。

核心**

string.decode(*encoding*)   # from to unicode.encode(*encoding*)   # from to

執行步驟:

1、將原始文字的編碼方

式儲存為utf-8格式。

2、在python程式中匯入文字

fr = open('all.txt','r')   #讀寫模式:r-唯讀;r+讀寫;w-新建(會覆蓋原有檔案);更多資訊見參考文章2

all_utf8 = fr.read()

all_utf8 #螢幕上會出現類似「\xe86\xe95\xa3」的文字

3、將

原始文字的編碼方式解碼,即

轉換為unicode。可使用下面兩種方式的任意一種。

all_uni = all_utf8.decode("utf-8")

all_uni = unicode(all_utf8, 'utf-8')
4、比較轉換前和轉換後的文字

print all_utf8   #亂碼

print all_uni #中文

處理

非英文文字,最重要的點是:

1、decode early (盡早decode, 將

檔案中的內容轉化成 unicode 再進行下一步處理)

def to_unicode_or_bust(obj, encoding='utf-8'):

if isinstance(obj, basestring): #檢查是否為字串

if not isinstance(obj, unicode): #檢查編碼是否為unicode

obj = unicode(obj, encoding) #使用utf-8,將文字轉換為unicode

return obj

'''檢查乙個obejct是否是字串,如是非unicode的字串將其轉換成unicode。'''

2、unicode everywhere (程式內部處理都用unicode)

to_unicode_or_bust(all_utf8)

3、encode late (最後encode回所需的encoding, 例如把最終結果寫進結果檔案)

file = open('all_out.txt','wb')   #建立乙個all_out的txt檔案

file.write(all_uni.encode('utf-8')) #用utf-8編碼方式儲存all_uni

file.close() #儲存完畢

其它:

1、檢視python預設的編碼方式

import sys

sys.getdefaultencoding() # 'ascii'

2、更改python預設的編碼方式

sys.setdefaultencoding('utf-8')

3、使用codecs模組匯入文件:用codecs提供的open方法,可以指定開啟的檔案的語言編碼,並在讀取的時候自動轉換為unicode。

import codecs

file = open('all_out.txt', 'r', encoding = 'utf-8')

4、bom

windows下載入文字,有時會出現bom頭,即在檔案的開頭有'特殊'的記號標識該檔案屬於utf-8編碼。

使用codecs模組,檢查是否有bom頭的方法

fr = open('all.txt','r')

sample = fr.read(4)

sample.startswith(codecs.bom_utf16_le) or sample.startswith(codecs.bom_utf16_be)

utf-16格式的文字在decode是會自動刪除bom;utf-8文字刪除bom使用如下**

string.decode('utf-8-sig')

------------------------------

----

----------

解決問題二:

匯入包含中文的py file

------------------------

--------------------

python預設使用ascii編碼作為標準編碼方式,所以需要在文件的第一行或第二行註明我們的編碼方式。

核心**(標註方式)

# coding=
或者

#!/usr/bin/python

# -*- coding: -*-

或者

#!/usr/bin/python

# vim: set fileencoding=:

這裡只要符合正規表示式"^[ \t\v]*#.*?coding[:=][ \t]*([-_.a-za-z0-9]+)"即可,所以等號也可以替換為冒號

執行步驟

1、在記事本中輸入以下**,使用ansi格式儲存,命名為test.py

# coding=utf-8

s = "中文"

print s

2、在python中匯入py file

import test

其它:我們也可以使用普通文本來表示編碼,例如在記事本中輸入:

# this python file uses the following encoding: utf-8

s = "中文"

print s

參考文章:

1、字元編碼筆記:ascii,unicode和utf-8

2、python:open/檔案操作

3、python處理中文的時候的一些小技巧

4、pep 263 -- defining python source code encodings

Python讀取 csv檔案中文亂碼處理

需求 按行解析讀取csv檔案存入關係型資料庫 主要是中文字型解析 遇到的問題 直接解析出來的資料為list形式,而且編碼格式為unicode 解決問題 前提了解 中文編碼的規則 gb2312字串在python內部的表示是unicode編碼,在做編碼轉換時,通常需要以unicode作為中間編碼,即先將...

Python處理中文

用python寫了個從一堆中文微博中抽取電影票房資料的程式,處理中文編碼問題非常麻煩,有以下經驗 1,在正規表示式中的中文應該用 u x的形式,正規表示式字串還要以ur為字首 u表示unicode,r表示raw,即忽略c 形式的轉義字元 2,各種編碼都統一成utf8的時候世界終於清靜了 4,原始碼開...

python中文處理

1.多位元組問題必須要全部轉成unicode再處理,否則就會有問題,比如中文gbk編碼的 和珅 其中的珅的後半位元組和 的一樣的,所以在處理的時候會有問題,如下我們用re.split來分割 用正則分隔某個字串 def split str,patternlist unicodestr str.deco...