我想使用Python在文件中匹配模式之前和之后提取文本
答案 0 :(得分:0)
这是您的意思吗?您想按值分割字符串吗?
with open("filename.extension",'r') as file:
txt = "has available versions"
for line in file:
if txt in line:
part_one = line.split(txt)[0]
part_two = line.split(txt)[1]
您可以将值放入字典中
import re
dict = {}
l = "[{'entity_class':'some value','value1':'1', 'value2':'2', 'entity_model':'some_value', 'value 3': '3'}]"
list = l.split(",")
for entry in list:
key = entry.split(":")[0]
value = entry.split(":")[1]
key = re.sub('[^A-Za-z0-9]+', '', key)
value = re.sub('[^A-Za-z0-9]+', '', value)
dict[key] = value
print(dict)