2014-07-15 184 views
-1

據說這是爲了驗證線「廉政」中有一個IP,然後打印行,但我收到類型錯誤:不支持的操作類型「列表」和

TypeError: unsupported operand types for 'list' & 'int' 

我不能想到了解決辦法對於我現在的代碼,任何人都可以伸出援手嗎?

import sys 

import re 
match = 0 

def CheckIP(ip, filter): 

    match = re.compile(b"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$", ip) 
    if match == None: 
     print ("No IP's in this file!") 
     sys.exit(0) 
    else: 
     pass 

with open('text.log') as f: 

    x=[] 
    count = 0 
    for l in f: 
     x = l.strip().split("\t")[5:8] # Grab the elements you want... 
     x.pop(1) #... but remove the element you don't 
     CheckIP(x, match) 
     print(" ".join(x)) # Now print them 
     print('\n') 
     count += 1 
     if count == 20: 
       print("\n\n\n\n\n-----NEW GROUPING OF 20 RESULTS-----\n\n\n\n\n\n") 
       count = 0 

print(count) 

修改:

import sys 
import re 

with open('text.log') as f: 

    x=[] 
    count = 0 
    for l in f: 
     x = l.strip().split("\t")[5:8] # Grab the elements you want... 
     x.pop(1) #... but remove the element you don't 
     match = re.compile("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$", x) 
     if any(lambda s: match.search(s) for s in x): 
      pass 
     else: 
      print ("No IP's in this file!") 
      sys.exit(0) 
     print(" ".join(x)) # Now print them 
     print('\n') 
     count += 1 
     if count == 20: 
       print("\n\n\n\n\n-----NEW GROUPING OF 20 RESULTS-----\n\n\n\n\n\n") 
       count = 0 

print(count) 

錯誤:

 Traceback (most recent call last): 
     File "D:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\Microsoft\Python Tools for Visual Studio\2.0\visualstudio_py_util.py", line 76, in exec_file 
    exec(code_obj, global_variables) 

    File "C:\Users\marco\Documents\Visual Studio 2013\Projects\PythonApplication4\ 
PythonApplication4\a2.py", line 10, in <module>match = re.compile("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$", x) 

    File "D:\Python34\lib\re.py", line 219, in compile 
    return _compile(pattern, flags) 

    File "D:\Python34\lib\re.py", line 275, in _compile 
    bypass_cache = flags & DEBUG 

TypeError: unsupported operand type(s) for &: 'list' and 'int' 
Press any key to continue . . . 
+0

向我們展示你得到完整的錯誤消息。 – jwodder

+0

@jwodder道歉,增加了錯誤。 – user3788715

+2

爲什麼你將'match'傳遞給'CheckIP()'?這成爲你的'過濾器'參數,這是從來沒有使用。 – chrisaycock

回答

8

您使用re.compile不正確。第二個參數應該是一個整數,通過將re標誌按位進行或運算而形成,而不是與要匹配的字符串列表。

做你出現什麼最簡單的方法是試圖做的是:

pattern = re.compile( ### regex ###) 
if any(pattern.search(s) for s in ip): 
    pass 
else: 
    print ("No IP's in this file!") 
    sys.exit(0) 
+0

好吧,我嘗試了你的方法,現在將忽略這個功能,但我似乎收到了同樣的錯誤!我仍然在做同樣的事情錯了嗎?我添加了新的錯誤和修改後的代碼@jwodder? – user3788715

+0

@ user3788715:不要將'x'傳遞給're.compile'。只傳遞正則表達式,沒有別的:match = re.compile(「^(([0-9] | [1-9] [0-9] | 1 [0-9] {2} | 2 [0- 4] [0-9] | 25 [0-5])\){3}([0-9] |。[1-9] [0-9] | 1 [0-9] {2} | 2 [0-4] [0-9] | 25 [0-5])$「)' – jwodder

+0

好吧,我正在使用視覺工作室,我現在正在接收GeneratorExit發生。我無法找到任何關於如何解決這個問題的文檔,你有沒有使用它的經驗? 它發生在這條線上「如果有的話(lambda s:match.search(s)for s in x):」@jwodder – user3788715

相關問題