我想創(chuàng)建一個(gè)正則表達(dá)式來(lái)僅獲取以日期開(kāi)頭的行(忽略其他行)和帶有“前綴”字樣的行。正則表達(dá)式應(yīng)該是什么樣子的?我的txt文件中有以下結(jié)構(gòu): Prefix : 0051601 Data Material No. OS Hist. Nr/Controle Quant. Vlr.Unit. Vlr.Total ---------------------------------------------------------------------------------------------------------------------------------------- 13/01/2008 00101050 Lampada farol H5 24V 003 4863 2,000 9,870556 19,7411 ====== Total dia 13/01/2008 ====== Entradas : Saídas : 2,000 19,7411 -------------------------------------------------------------------主要代碼是:import glob, osimport reos.chdir("./txtfiles/")for file in glob.glob("*.txt"): with open(file) as f: content = f.readlines() # not working, just for test purpose result = re.match(r'Prefix', content, re.M|re.I) if result: print(content) else: print "no match found!"
2 回答

翻過(guò)高山走不出你
TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超3個(gè)贊
下面沒(méi)有re,假設(shè)開(kāi)始日期為日期的行是唯一/在位置 2 和 5 處的行...:
with open(file) as f:
for line in f:
if line[2]==line[5]=='/' or 'Prefix' in line:
print(line)
添加回答
舉報(bào)
0/150
提交
取消