python 正則匹配 csv檔案中特殊符號如■高風險 這樣的black block

來源:互聯網
上載者:User

標籤:尋找   inf   com   c++   search   bin   auto   儲存   檔案中   

各種符號必然在unicode中有儲存,根據unicode代碼匹配即可。

1.csv中特殊字元可以在excel的【插入符號】中尋找,然後記下該符號的Unicode代碼。如■ 這個實心方形的Unicode代碼為25A0:

2.需要查詢其他的代碼可以以此代碼為基礎搜尋,(當然本例用詞代碼即可):如unicode 25A0 python,可得到在各編碼方式的代碼,本例網址:

http://www.fileformat.info/info/unicode/char/25a0/index.htm  找python 即:

UTF-8 (hex) 0xE2 0x96 0xA0 (e296a0)
UTF-8 (binary) 11100010:10010110:10100000
UTF-16 (hex) 0x25A0 (25a0)
UTF-16 (decimal) 9,632
UTF-32 (hex) 0x000025A0 (25a0)
UTF-32 (decimal) 9,632
C/C++/Java source code "\u25A0"
Python source code u"\u25A0"

3.

# -*- coding: utf-8 -*-import rea=u"\u25A0"b=u"高風險"d="%s%s"%(a,b)d1=d.encode(‘GBK‘) #decode 以便匹配文本擷取內容,原為utf8p=re.compile(".*\s*%s\s*.*"%d1)with open ("b.csv","r") as r:    for i in r:    #print i    line=i.strip().split("#")[7]    print line    if p.search(line):    count+=1print "count =%s"%count

 

還需要認真的學習編碼方式:http://python.jobbole.com/86670/

python 正則匹配 csv檔案中特殊符號如■高風險 這樣的black block

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.