目前,我在下面导入了文本。
AS400 Storage Management -svc
AS400 Storage Management -svc 2
AS400 Hardware Management -sv 2
AS400 HA sync -app
AS400 HA sync -svc
Bank Users to Cognos
FTP to AS400
我需要使用Python修改它,使其看起来像这样:
(name eq 'AS400 Storage Managemnet -svc'
or name eq 'AS400 Storage Management -svc 2'
or name eq 'AS400 Hardware Management -sv 2'
or name eq 'AS400 HA sync -app'
or name eq 'AS400 HA sync -svc'
or name eq 'Bank Users to Cognos'
or name eq 'FTP to AS400'
我的python脚本是:
fwtext = []
with open("C:/Users/me/PycharmProjects/pythonProject/paloalto/TestText.txt") as f:
line = f.readline()
while line:
line = f.readline()
fwtext.append(line)
print("(name eq "+line+"")
with open("C:/Users/me/PycharmProjects/pythonProject/paloalto/fwsearch.txt", "w")as f:
for line in fwtext:
f.write("name eq '"+line+"'or ")
它显示在下面。我如何让Python操作我的文本以在eof中包含“(“文本的开头和”)”?我尝试了很多不同的方法来实现这一点,我对Python还是新手
name eq 'AS400 Storage Management -svc 2
'or name eq 'AS400 Hardware Management -sv 2
'or name eq 'AS400 HA sync -app
'or name eq 'AS400 HA sync -svc
'or name eq 'Bank Users to Cognos
'or name eq 'FTP to AS400
核心re库可以轻松完成这些更改
您还可以调用函数进行自定义替换
使用^{}
如果你想使用一个包,
re
可能是你最好的选择,正如上面的人所说。如果您想从头开始编写代码,我认为最简单的方法是将整个输入看作一大块\n
分隔的文本,然后使用一些基本的字符串操作:相关问题 更多 >
编程相关推荐