Я пытаюсь извлечь IP-адрес из asp
файла в Python, файл выглядит примерно так:Извлечение IP-адрес из файла
onInternalNet = (
isInNet(hostDNS, "147.163.1.0", "255.255.0.0") ||
isInNet(hostDNS, "123.264.0.0", "255.255.0.0") ||
isInNet(hostDNS, "137.5.0.0", "255.0.0.0") ||
isInNet(hostDNS, "100.01.02.0", "255.0.0.0") ||
isInNet(hostDNS, "172.146.30.0", "255.240.0.0") ||
isInNet(hostDNS, "112.268.0.0", "255.255.0.0") ||
Как я пытаюсь извлечь их это с регулярным выражением :
if re.compile(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", line):
Однако я получаю сообщение об ошибке:
Traceback (most recent call last):
File "pull_proxy.py", line 27, in <module>
write_to_file(extract_proxies(in_file), out_file)
File "pull_proxy.py", line 8, in extract_proxies
if re.compile(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", line):
File "C:\Python27\lib\re.py", line 194, in compile
return _compile(pattern, flags)
File "C:\Python27\lib\re.py", line 233, in _compile
bypass_cache = flags & DEBUG
TypeError: unsupported operand type(s) for &: 'str' and 'int'
Я не понимаю, почему я получаю g что ошибка, что я могу сделать с этим кодом, чтобы он извлекал информацию, как я этого хочу?
import re
def extract_proxies(in_file):
buffer = []
for line in in_file:
if re.compile(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", line):
print "{} appened to buffer.".format(line)
buffer.append(line)
else:
pass
return buffer
def write_to_file(buffer, out_file):
for proxy in buffer:
with open(out_file, "a+") as res:
res.write(proxy)
if __name__ == '__main__':
print "Running...."
in_file = "C:/Users/thomas_j_perkins/Downloads/test.asp"
out_file = "c:/users/thomas_j_perkins/Downloads/results.txt"
write_to_file(extract_proxies(in_file), out_file)
EDIT
Реализованный я не открыл файл:
import re
def extract_proxies(in_file):
buffer = []
for line in in_file:
if re.compile(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", line):
print "{} appened to buffer.".format(line)
buffer.append(line)
else:
pass
in_file.close()
return buffer
def write_to_file(buffer, out_file):
for proxy in buffer:
with open(out_file, "a+") as res:
res.write(proxy)
if __name__ == '__main__':
print "Running...."
in_file = "C:/Users/thomas_j_perkins/Downloads/PAC-Global-Vista.asp"
out_file = "c:/users/thomas_j_perkins/Downloads/results.txt"
write_to_file(extract_proxies(open(in_file, "r+")), out_file)
все еще получаю ту же ошибку:
Running....
Traceback (most recent call last):
File "pull_proxy.py", line 28, in <module>
write_to_file(extract_proxies(open(in_file)), out_file)
File "pull_proxy.py", line 8, in extract_proxies
if re.compile(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", line):
File "C:\Python27\lib\re.py", line 194, in compile
return _compile(pattern, flags)
File "C:\Python27\lib\re.py", line 233, in _compile
bypass_cache = flags & DEBUG
TypeError: unsupported operand type(s) for &: 'str' and 'int'
Вы никогда не открыть файл. Вы просто зацикливаете строку in_file. – eandersson
@eandersson Ha, я только что отредактировал это в вопросе –
как насчет отказа от регулярных выражений и просто разделить его по пространству? – karlosss