最近在分析日志时遇到了一个问题。你知道吗
“需要读取10GB大小的单行日志文件,并且必须打印所有IP地址”
问题:无法逐行读取以避免内存损坏。必须一个字接一个字。你知道吗
解决方案:
#!/usr/bin/python
import re
def getIP():
ip = re.compile('\d+|\\.')
out = []
with open("./ipaddr","r") as f:
while True:
c = f.read(1)
if not c:
break
if ip.match(c):
out.append(c)
for i in range(14):
c = f.read(1)
if ip.match(c):
out.append(c)
else:
if out:
yield "".join(out)
out = []
print str([ipad for ipad in getIP()])
有什么想法可以简化吗??你知道吗
这应该做到:
相关问题 更多 >
编程相关推荐