Python 3.x 如何通过Python在属性文件中使用带有正则表达式字符串的正则表达式模式进行搜索
我确实有一个Key=Value对的属性文件 属性文件名=“johny\u johny\u yes\u papa.properties” 我有一个字符串列表,如下所示-Python 3.x 如何通过Python在属性文件中使用带有正则表达式字符串的正则表达式模式进行搜索,python-3.x,regex,list,file,properties,Python 3.x,Regex,List,File,Properties,我确实有一个Key=Value对的属性文件 属性文件名=“johny\u johny\u yes\u papa.properties” 我有一个字符串列表,如下所示- List_papa = ['org','johnny','jimmy','yes','papa'] 我的目标是在=(equal)符号之后的属性文件中搜索上面列表中的字符串,并打印该行 我的代码- ENV_PROP = "johhny_johhny_yes_papa.properties" List_papa
List_papa = ['org','johnny','jimmy','yes','papa']
我的目标是在=(equal)符号之后的属性文件中搜索上面列表中的字符串,并打印该行
我的代码-
ENV_PROP = "johhny_johhny_yes_papa.properties"
List_papa = ['org','johnny','jimmy','yes','papa']
with open(ENV_PROP) as f:
file_content = f.read()
contents = file_content.split('\n')
for line in contents:
for user in List_papa:
if (re.findall("\\b"+user+"\\b",line)):
print(line)
prop.org.size = 5
user.id.name = johnny
name.account.id = jimmy
cassandra.user.name = yes
kakfa.user.name = org
user.id.name = johnny
name.account.id = jimmy
cassandra.user.name = yes
kakfa.user.name = org
ENV_PROP = "johhny_johhny_yes_papa.properties"
List_papa = ['org','johnny','jimmy','yes','papa']
with open(ENV_PROP) as f:
file_content = f.read()
contents = file_content.split('\n')
for line in contents:
regex_line = re.findall(r"(?<==).+$",line)
for user in List_papa:
if (re.findall("\\b"+user+"\\b",regex_line)):
print(line)
user.id.name = johnny
name.account.id = jimmy
cassandra.user.name = yes
kakfa.user.name = org
我的输出-
ENV_PROP = "johhny_johhny_yes_papa.properties"
List_papa = ['org','johnny','jimmy','yes','papa']
with open(ENV_PROP) as f:
file_content = f.read()
contents = file_content.split('\n')
for line in contents:
for user in List_papa:
if (re.findall("\\b"+user+"\\b",line)):
print(line)
prop.org.size = 5
user.id.name = johnny
name.account.id = jimmy
cassandra.user.name = yes
kakfa.user.name = org
user.id.name = johnny
name.account.id = jimmy
cassandra.user.name = yes
kakfa.user.name = org
ENV_PROP = "johhny_johhny_yes_papa.properties"
List_papa = ['org','johnny','jimmy','yes','papa']
with open(ENV_PROP) as f:
file_content = f.read()
contents = file_content.split('\n')
for line in contents:
regex_line = re.findall(r"(?<==).+$",line)
for user in List_papa:
if (re.findall("\\b"+user+"\\b",regex_line)):
print(line)
user.id.name = johnny
name.account.id = jimmy
cassandra.user.name = yes
kakfa.user.name = org
它实际上是在打印所有内容,因为我的if条件在所有行中都是真的
我的预期输出是-
ENV_PROP = "johhny_johhny_yes_papa.properties"
List_papa = ['org','johnny','jimmy','yes','papa']
with open(ENV_PROP) as f:
file_content = f.read()
contents = file_content.split('\n')
for line in contents:
for user in List_papa:
if (re.findall("\\b"+user+"\\b",line)):
print(line)
prop.org.size = 5
user.id.name = johnny
name.account.id = jimmy
cassandra.user.name = yes
kakfa.user.name = org
user.id.name = johnny
name.account.id = jimmy
cassandra.user.name = yes
kakfa.user.name = org
ENV_PROP = "johhny_johhny_yes_papa.properties"
List_papa = ['org','johnny','jimmy','yes','papa']
with open(ENV_PROP) as f:
file_content = f.read()
contents = file_content.split('\n')
for line in contents:
regex_line = re.findall(r"(?<==).+$",line)
for user in List_papa:
if (re.findall("\\b"+user+"\\b",regex_line)):
print(line)
user.id.name = johnny
name.account.id = jimmy
cassandra.user.name = yes
kakfa.user.name = org
我想在属性文件的after=(equalto)符号中搜索列表元素
我在正则表达式下面得到了这个
(?<==).+$
(?使用str.endswith
可以将多个参数作为元组传递给str.endswith
Ex:
List_papa = ('org','johnny','jimmy','yes','papa') #Note! List_papa is a tuple
with open(filename) as infile:
for line in infile:
line = line.strip()
if line.endswith(List_papa):
print(line)
输出:
List_papa = ('org','johnny','jimmy','yes','papa') #Note! List_papa is a tuple
with open(filename) as infile:
for line in infile:
line = line.strip()
if line.endswith(List_papa):
print(line)