使用 '\n' 将正则表达式结果放在一行上以进行分隔。 '+' 不受支持的操作数
Using '\n' to place regex findings on a line for seperation. '+' unsupported operand
所以昨天我让我的正则表达式逐行打印。今天我删除了我的 if 语句,将 "result" 设置为等于正则表达式,这样我就可以将它附加到我的列表中。好吧,它破坏了我的 (dns+'\n')。也许相关或不相关。显示给我的错误如下。
dns.append(result+'\n')
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
代码:
import re
import pandas as pd
dns = []
addstrip = []
with open('ns.txt', 'r') as file:
lines = file.read().splitlines()
for line in lines:
#result = (re.search(r'(\W\S+)(\.)(\S+)(\.)(\S\S\S+)', line))
result = (re.search(r'^(add lb vserver) (\S+)(\.)(\S+)(\.)(\S+)', line))
dns.append(result+'\n')
print(dns)
with open('dnsout.txt', 'w') as f:
f.writelines(str(dns))
没有'\n'的输出
[None]
[None, None]
[None, None, None]
[None, None, None, None]
[None, None, None, None, None]
[None, None, None, None, None, None]
旧代码:
dns = []
with open('ns.txt', 'r') as file:
lines = file.read().splitlines()
for line in lines:
if re.search(r'^(add lb vserver )(\S+) (\S+) (\S+)(.+)$', line):
dns.append(line)
print(dns)
with open('dnsout.txt', 'w') as f:
f.writelines(lines)
'None' 之后的后续输出:
match='add lb vserver SSL_INT_unucrepSL.oncologysupply.c>, <_sre.SRE_Match object; span=(0, 51), match='add lb vserver SSL_EXT_unucrepSL.oncologysupply.c>, <_sre.SRE_Match object; span=(0, 57), match='add lb vserver SSL_INT_hotfixunucrepsl.oncologysu>, <_sre.SRE_Match object; span=(0, 57), match='add lb vserver SSL_EXT_hotfixunucrepsl.oncologysu>, <_sre.SRE_Match object; span=(0, 57), match='add lb vserver SSL_EXT_hotfixunucwcfsl.oncologysu>, <_sre.SRE_Match object; span=(0, 57), match='add lb vserver SSL_INT_hotfixunucwcfsl.oncologysu>, <_sre.SRE_Match object;
在上面的代码中,我实际上并没有打印出正则表达式的结果,而是整行。输出是逐行的,虽然也许 bc regex 结果是不同的数据类型?
Pandas 稍后将在下面的代码中使用,因此您可以忽略。如何按行分隔?我的猜测是我需要将它转换为可以采用 +'\n' 的其他类型。感谢您提供任何帮助、提示或技巧。谢谢阅读。
根据您的评论,
import re
import pandas as pd
dns = []
addstrip = []
with open('ns.txt', 'r') as file:
lines = file.read().splitlines()
for line in lines:
result = (re.search(r'^(add lb vserver) (\S+)(\.)(\S+)(\.)(\S+)', line))
if result:
dns.append(result.group(x)+'\n') # x is the group number that you would need.
print(dns, end=",")
with open('dnsout.txt', 'w') as f:
f.writelines(str(dns))
所以昨天我让我的正则表达式逐行打印。今天我删除了我的 if 语句,将 "result" 设置为等于正则表达式,这样我就可以将它附加到我的列表中。好吧,它破坏了我的 (dns+'\n')。也许相关或不相关。显示给我的错误如下。
dns.append(result+'\n')
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
代码:
import re
import pandas as pd
dns = []
addstrip = []
with open('ns.txt', 'r') as file:
lines = file.read().splitlines()
for line in lines:
#result = (re.search(r'(\W\S+)(\.)(\S+)(\.)(\S\S\S+)', line))
result = (re.search(r'^(add lb vserver) (\S+)(\.)(\S+)(\.)(\S+)', line))
dns.append(result+'\n')
print(dns)
with open('dnsout.txt', 'w') as f:
f.writelines(str(dns))
没有'\n'的输出
[None]
[None, None]
[None, None, None]
[None, None, None, None]
[None, None, None, None, None]
[None, None, None, None, None, None]
旧代码:
dns = []
with open('ns.txt', 'r') as file:
lines = file.read().splitlines()
for line in lines:
if re.search(r'^(add lb vserver )(\S+) (\S+) (\S+)(.+)$', line):
dns.append(line)
print(dns)
with open('dnsout.txt', 'w') as f:
f.writelines(lines)
'None' 之后的后续输出:
match='add lb vserver SSL_INT_unucrepSL.oncologysupply.c>, <_sre.SRE_Match object; span=(0, 51), match='add lb vserver SSL_EXT_unucrepSL.oncologysupply.c>, <_sre.SRE_Match object; span=(0, 57), match='add lb vserver SSL_INT_hotfixunucrepsl.oncologysu>, <_sre.SRE_Match object; span=(0, 57), match='add lb vserver SSL_EXT_hotfixunucrepsl.oncologysu>, <_sre.SRE_Match object; span=(0, 57), match='add lb vserver SSL_EXT_hotfixunucwcfsl.oncologysu>, <_sre.SRE_Match object; span=(0, 57), match='add lb vserver SSL_INT_hotfixunucwcfsl.oncologysu>, <_sre.SRE_Match object;
在上面的代码中,我实际上并没有打印出正则表达式的结果,而是整行。输出是逐行的,虽然也许 bc regex 结果是不同的数据类型?
Pandas 稍后将在下面的代码中使用,因此您可以忽略。如何按行分隔?我的猜测是我需要将它转换为可以采用 +'\n' 的其他类型。感谢您提供任何帮助、提示或技巧。谢谢阅读。
根据您的评论,
import re
import pandas as pd
dns = []
addstrip = []
with open('ns.txt', 'r') as file:
lines = file.read().splitlines()
for line in lines:
result = (re.search(r'^(add lb vserver) (\S+)(\.)(\S+)(\.)(\S+)', line))
if result:
dns.append(result.group(x)+'\n') # x is the group number that you would need.
print(dns, end=",")
with open('dnsout.txt', 'w') as f:
f.writelines(str(dns))