我遇到了一个奇怪的错误,在该错误中我使用请求库获取MIB文件的文本,而regex最初不想使用它。我正在尝试用任何内容替换空白行。
此代码可以找到我要正确替换的内容。
mib = r.text
blanklines = re.compile(r'\n+\s', re.DOTALL)
for line in blanklines.finditer(mib):
...
此代码未对字符串进行任何修改。
mib = r.text
blanklines = re.compile(r'\n+\s', re.DOTALL)
mib = blanklines.sub('\n', mib)
此代码正在完全按照我的要求修改字符串。
mib = r.text
with open('rtext.txt', 'w') as tfile:
tfile.write(mib)
with open('rtext.txt','r') as tfile:
mib = tfile.read()
blanklines = re.compile(r'\n+\s', re.DOTALL)
mib = blanklines.sub('\n', mib)
我不认为在周围使用这项工作很糟糕,但是我希望这里的人可以告诉我我做错了什么。我认为它必须与响应文本一起使用,因为它在写入文件然后再读回之后效果很好。
答案 0 :(得分:0)
As commented the response had quite a few line breaks that were written as \r\n and I just overlooked them.
Making one change mib = r.text.replace('\r', '')
fixed the whole problem for me.
And I'm not necessarily trying to parse the entire MIB file so what I have working at this point is going to work for me. I'm really just checking a pcapng file for traps and if we don't have the OID info stored grabbing it from a repo at circitor.fr/Mibs.
Also if someone has a better repo than that I'd appreciate it because it can be pretty slow.