2014-11-06 55 views
-2

我有一個緩存日誌文件,我必須刪除其中包含「.js?」的url的行 「.gif?」,「.png?」必須予以消除。在url中尋找一個模式在python中

logfile=open('/home/prasanna/Downloads/processed_file','r') 
cleanfile=open('/home/prasanna/Downloads/cleaned_file','a') 
with logfile: 
    for line in logfile: 
     line_words=line.split() 
     url=line_words[6].split('.') 
     #pattern if_condition 
       cleanfile.write(line) 
cleanfile.close() 
logfile.close() 

我需要從processed_file線寫入到乾淨的文件每當processed_file犯規的線上面已經說過圖案

例如: 1168414758.369 723 80.126.67.6 TCP_MISS/304 380 GET http://c.msn.com/c.gif?[07lKw.F:jbQg5CY03lJ8T.] - DIRECT/207.46.216.62 -

1168416013.376 621 233.7.37.201 TCP_MISS/304 162 GET http://mobile9.us.intellitxt.com/v3/func_033.js?[15zZlncWMGXv5PQNupu.tC] - DIRECT/205.147.84.25 -

+0

你是什麼意思的消除線?你能否擴展你的例子以及預期的產出? – 2014-11-06 01:58:51

+1

你到目前爲止嘗試過什麼?這聽起來更像是「爲我做這個!」比聽起來像「我該怎麼做?」 – 2014-11-06 02:00:13

+0

從某種意義上說,我已經將226789行文件重寫爲其他文件,但我無法做到這種模式。 – 2014-11-06 02:01:04

回答

1

如果通過消除你的意思是不寫行到清理文件,然後一個簡單的檢查應該做到這一點。

logfile=open('/home/prasanna/Downloads/processed_file','r') 
cleanfile=open('/home/prasanna/Downloads/cleaned_file','a') 
with logfile: 
    for line in logfile: 
     line_words=line.split() 
     url=line_words[6].split('.') 
     if "gif?" not in line and ".png?" not in line and ".js?" not in line: 
       cleanfile.write(line) 
cleanfile.close() 
logfile.close() 
+0

我的意思是將行寫入一個新文件,其中沒有該模式的行。 – 2014-11-06 03:38:09

+0

上面的代碼應該爲你做這個工作 – chettyharish 2014-11-06 03:40:18

+0

謝謝你的回答 – 2014-11-06 03:48:10

0

這是這麼難?

for line in file: 
    if ".gif" in line or ".png" in line or ".js" in line: 
     line = "" 
    else: 
     pass 
+0

感謝您的回答 – 2014-11-06 03:47:11