據說這是爲了驗證線「廉政」中有一個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 . . .
向我們展示你得到完整的錯誤消息。 – jwodder
@jwodder道歉,增加了錯誤。 – user3788715
爲什麼你將'match'傳遞給'CheckIP()'?這成爲你的'過濾器'參數,這是從來沒有使用。 – chrisaycock