使用python從一段文字中使用正則匹配自己需要的文字,如果使用search,只會進行一次匹配,group(0)裡面的內容是全匹配,group(1)裡面的內容是括號裡面的子正則。如果需要全部匹配就使用findall,我這裡的就是全匹配,返回的是乙個列表,每一項內容就是括號裡的字正則匹配內容。
#!/usr/bin/env python
import re
import sys
import json
class
regeximg
(object):
def__init__
(self):
self.pattern = re.compile(r'
with open(dict, 'r') as fd, open(input, 'r') as fin, open(output, 'w') as fout:
for line in fd:
parts = line.rstrip().split('\t')
if len(parts) != 2:
print
"wrong format line:%s" % line
continue
dt[parts[0]] = parts[1]
for line in fin:
parts = line.rstrip().split('\t')
if len(parts) != 3:
print
"wrong format line:%s" % line
continue
ideaid = parts[0]
material = parts[2]
matchs = regex_img.process(material)
if len(matchs) != 3:
print
"wrong img num:%d" % len(matchs)
print matchs
continue
if ideaid not
in dt:
print
"ideaid:%s has not its planid" % (ideaid)
continue
tmp_dt = {}
tmp_dt["planid"] = dt[ideaid]
tmp_dt["img_src"] = matchs
json_str = json.dumps(tmp_dt)
fout.write("%s\n" % json_str)
if __name__ == "__main__":
main()
python使用sqlite示例
sqlite是一種嵌入式資料庫,它的資料庫就是乙個檔案。python就內建了sqlite3,所以,在python中使用sqlite,不需要安裝任何東西,直接使用。操作關聯式資料庫,首先需要連線到資料庫,乙個資料庫連線稱為connection 連線到資料庫後,需要開啟游標,稱之為cursor,通過cu...
Python 正則使用
單字修飾符 1.匹配任意字元,除了換行符 2.用來表示一組字元,單獨列出 abc 匹配 a b 或 c 3.d 匹配任意數字,等價於 0 9 4.d 匹配任意非數字 5.w 匹配字母數字及下劃線 6.w 匹配非字母數字及下劃線 7.s 匹配任意空白字元,等價於 t n r f 8.s 匹配任意非空字...
使用Python寫spark 示例
python寫spark我認為唯一的理由就是 你要做資料探勘,ai相關的工作。因為很多做數挖的他們的基礎語言都是python,他們如果重新學scala比較耗時,而且,python他的強大類庫是他的優勢,很多演算法庫只有python有。python的安裝 解壓python包,在環境變數裡面配上bin的...