python查詢中文字元

2021-05-22 09:25:42 字數 3916 閱讀 1062

#filename seek.py

import unicodedata

import sys

import os

class seek():

"""功能:查詢中文,並替換成指定字元或字串

使用方法:python指令碼用法

引數說明:

-d    : 檔案目錄(絕對或相對路徑)(預設為指令碼所在目錄)

-t    : 檔案型別(檔名字尾,如.jsp;.txt)(預設為所有檔案)

-sf   : 是否包括子目錄(y|n)(option,預設不包括子目錄)

-r    : 輸出檔名(option,預設為'chinesecharacter.txt'),位於指令碼目錄下

-encoding:檔案字元編碼(預設為utf-8)

"""def __init__(self):

"""初始化查詢程式

引數解析

"""#todo:

self.d = '.'

self.sf = 'n'

self.t = 'all'

self.r = 'chinesecharacter.txt'

self.encoding = 'utf-8'

varl = 0

for ar in sys.argv:

if ar == '-d':

self.d = sys.argv[varl+1]

continue

if ar =='-sf':

sf = sys.argv[varl+1].upper()

if (sf == 'y') | (sf == 'n'):

self.sf = sf

else:

print('input error with sf parameter')

continue

if ar=='-r':

self.r = sys.argv[varl+1]

continue

if ar=='-t':

self.t = sys.argv[varl+1]

continue

if ar == '-encoding':

self.encoding = sys.argv[varl+1]

continue

varl+=1

def seeking(self):

""""

開始查詢字元

"""try:

#output file

self.rfile = open(self.r,'w',encoding=self.encoding)

#start seek

for f in os.listdir(self.d):

path = os.path.join(self.d,f)

if self.__isfile(path):

if self.t != 'all':

if f.endswith(self.t):

self.__seek(path)

else:

self.__seek(path)

elif self.__isdir(path) and self.sf == 'y' :

#seek the sub folder when the self.sf equals 'y'

self.start(path)

except exception as error:

print('seek error %s' % error)

finally:

self.__close()

def __close(self):

"""關閉檔案及輸入流和輸出流

"""#close the stream and file

self.rfile.close()

def __isfile(self,file):

#return os.path.isfile(file)

def __isdir(self,path):

#return os.path.isdir(path)

def __openfile(self,file):

pass

def __closefile(self,file):

file.close()

def __seek(self,file):

"""查詢

"""#seek character

fileobj = open(file,'r',encoding=self.encoding)

linelist = fileobj.readlines()

#塊注釋標記

blockcomment = 'finish'

try:

isc = false

for line in linelist:

#查詢出注釋部分,並跳過

##跳過'/*'和'*/'中的內容,處理剩餘的內容

if blockcomment == 'start':

#塊注釋內容

index = line.find('*/')

if index != -1:

blockcomment = 'finish'

#塊注釋結束

#處理當前行'*/'後的內容

line = line[index+2:]

else:

#仍處於塊注釋內容中,跳過

continue

if line.startswith('//'):

#行注釋

#跳過行

continue

if line.startswith('/*'):

#塊注釋開始

blockcomment = 'start'

continue

#查詢字元

indextag = 0;

for s in line:

sindex = line.index(s)

try:

#將不是latin開頭的字元都找出來

if unicodedata.name(s).startswith('cjk') == true:

#todo

#content = linelist.index(line)+1+s

isc = true

#如果兩個字元間隔大於1,表示為不連續的中文

if (sindex - indextag) > 1 :

self.__writefile('/t'+s)

else:

self.__writefile(s)

indextag = sindex

except exception as error:

print('seek character error : %s in %s' % (error,fileobj.name))

continue

if isc:

for t in range(8):

self.__writefile('/t')

self.__writefile('line:')

self.__writefile('%d' % (linelist.index(line)+1))

self.__writefile('/n')

isc = false

finally:

self.__writefile('/n')

self.__writefile('------------'+fileobj.name)

self.__writefile('/n')

fileobj.close()

def __writefile(self,content):

self.rfile.write(content)

if __name__ == '__main__':

seek=seek()

seek.seeking()

Python提取中文字元

寫這個jupyter的原因是好幾次自己爬完新聞之後,發現中間有些是html標籤 或者其他多餘的英文本元,自己也不想保留,那麼這時候乙個暴力簡單的方法就是使用 unicode 範圍 u4e00 u9fff 來判別漢字 unicode 分配給漢字 中日韓越統一表意文字 的範圍為 4e00 9fff 目前...

python中文字元擷取亂碼

python學習中 python中關於中文字串擷取的問題 中文字元擷取亂碼 在python中乙個中文字元佔三個英文本元,看以下 print str 0 6 擷取啤酒兩個中文字元,需要從0開始截到6 print str 0 5 輸出 啤酒啤 就會出現亂碼 usr bin python coding u...

python提取中文字元 Python提取中文字元

寫這個jupyter的原因是好幾次自己爬完新聞之後,發現中間有些是html標籤 或者其他多餘的英文本元,自己也不想保留,那麼這時候乙個暴力簡單的方法就是使用 unicode 範圍 u4e00 u9fff 來判別漢字 unicode 分配給漢字 中日韓越統一表意文字 的範圍為 4e00 9fff 目前...