python регулярное выражение для поиска строки из многострочных фигурных скобок - PullRequest
1 голос
/ 19 июня 2020

У меня есть такая строка. Как создать словарь с первыми тегами в качестве ключа и всего после: в качестве значения?

test_string = """###Some Comment 
First-tags : 
{
  "tag1": {
    "tagKey1": "tagValue1",
    "tagKey2": "tagValue2"
  },
  "tag2": {
    "tagKey1": "tagValue1",
    "tagKey2": "tagValue2"
  }
  so on .....
} 
"""

пример: ключ будет First-tags, а значение будет

{
  "tag1": {
    "tagKey1": "tagValue1",
    "tagKey2": "tagValue2"
  },
  "tag2": {
    "tagKey1": "tagValue1",
    "tagKey2": "tagValue2"
  }
  so on .....
} 

[Edit: строковые данные находятся в файле. Проблема состоит в том, чтобы прочитать из файла и создать словарь, где ключ будет комментарием, а значением будет Json данные]

например, файл будет иметь:

###Some Comment 
    First-tags : 
    {
      "tag1": {
        "tagKey1": "tagValue1",
        "tagKey2": "tagValue2"
      },
      "tag2": {
        "tagKey1": "tagValue1",
        "tagKey2": "tagValue2"
      }
      so on .....
    } 


###2nd Comment 
    Second-tags : 
    {
      "tag1": {
        "tagKey1": "tagValue1",
        "tagKey2": "tagValue2"
      },
      "tag2": {
        "tagKey1": "tagValue1",
        "tagKey2": "tagValue2"
      }
      so on .....
    } 

###Some other Comment 
    someother-tags : 
    {
      "tag1": {
        "tagKey1": "tagValue1",
        "tagKey2": "tagValue2"
      },
      "tag2": {
        "tagKey1": "tagValue1",
        "tagKey2": "tagValue2"
      }
      so on .....
    } 

Ответы [ 2 ]

1 голос
/ 19 июня 2020

Вы можете использовать это регулярное выражение, которое будет соответствовать последнему набору символов слова (включая -) перед : в группе 1, а затем все остальное до следующего комментария (###) или конца строка в группу 2:

([\w-]+)\s*:\s*(.*?)(?=\s*###|$)

Затем вы можете создать словарь, перебирая две группы для каждого совпадения в строке:

import re

test_string = """
###Some Comment 
    First-tags : 
    {
      "tag1": {
        "tagKey1": "tagValue1",
        "tagKey2": "tagValue2"
      },
      "tag2": {
        "tagKey1": "tagValue1",
        "tagKey2": "tagValue2"
      }
      so on .....
    } 


###2nd Comment 
    Second-tags : 
    {
      "tag1": {
        "tagKey1": "tagValue1",
        "tagKey2": "tagValue2"
      },
      "tag2": {
        "tagKey1": "tagValue1",
        "tagKey2": "tagValue2"
      }
      so on .....
    } 

###Some other Comment 
    someother-tags : 
    {
      "tag1": {
        "tagKey1": "tagValue1",
        "tagKey2": "tagValue2"
      },
      "tag2": {
        "tagKey1": "tagValue1",
        "tagKey2": "tagValue2"
      }
      so on .....
    }
"""
res = {}
for match in re.finditer(r'([\w-]+)\s*:\s*(.*?)(?=\s*###|$)', test_string, re.S):
    res[match.group(1)] = match.group(2)

print(res)

Вывод:

{
 'First-tags': '{\n      "tag1": {\n        "tagKey1": "tagValue1",\n        "tagKey2": "tagValue2"\n      },\n      "tag2": {\n        "tagKey1": "tagValue1",\n        "tagKey2": "tagValue2"\n      }\n      so on .....\n    }',
 'Second-tags': '{\n      "tag1": {\n        "tagKey1": "tagValue1",\n        "tagKey2": "tagValue2"\n      },\n      "tag2": {\n        "tagKey1": "tagValue1",\n        "tagKey2": "tagValue2"\n      }\n      so on .....\n    }',
 'someother-tags': '{\n      "tag1": {\n        "tagKey1": "tagValue1",\n        "tagKey2": "tagValue2"\n      },\n      "tag2": {\n        "tagKey1": "tagValue1",\n        "tagKey2": "tagValue2"\n      }\n      so on .....\n    }'
}

Обновление

Если вы также используете sh для получения комментариев, вы можете использовать этот код:

res = {}
for match in re.finditer(r'###([^\n]+)\s*([\w-]+)\s*:\s*(.*?)(?=\s*###|$)', test_string, re.S):
    res[match.group(1)] = { match.group(2) : match.group(3) }

print(res)

Вывод:

{
 'Some Comment ': {
   'First-tags': '{\n      "tag1": {\n        "tagKey1": "tagValue1",\n        "tagKey2": "tagValue2"\n      },\n      "tag2": {\n        "tagKey1": "tagValue1",\n        "tagKey2": "tagValue2"\n      }\n      so on .....\n    }'
 },
'2nd Comment ': {
   'Second-tags': '{\n      "tag1": {\n        "tagKey1": "tagValue1",\n        "tagKey2": "tagValue2"\n      },\n      "tag2": {\n        "tagKey1": "tagValue1",\n        "tagKey2": "tagValue2"\n      }\n      so on .....\n    }'
 },
 'Some other Comment ': {
  'someother-tags': '{\n      "tag1": {\n        "tagKey1": "tagValue1",\n        "tagKey2": "tagValue2"\n      },\n      "tag2": {\n        "tagKey1": "tagValue1",\n        "tagKey2": "tagValue2"\n      }\n      so on .....\n    }'
 }
}
1 голос
/ 19 июня 2020

Итак, здесь я пытаюсь преобразовать строку в JSON

Но для того, чтобы это работало, моя str должна быть JSON и ничего больше

, поэтому я нахожу первый { и взяв строку оттуда

import json

my_str = '''
First-tags : 
{
  "tag1": {
    "tagKey1": "tagValue1",
    "tagKey2": "tagValue2"
  },
  "tag2": {
    "tagKey1": "tagValue1",
    "tagKey2": "tagValue2"
  }
  }
  '''
# find the first {
i = my_str.index('{')
my_str = my_str[i:] # trim the string so that only dict is left
my_dict = dict(json.loads(my_str)) # create JSON and then convert that to dict
print(my_dict) # n'joy

Если хотите, вы также можете найти конец JSON и обрезать str (find })

обновление решения на основе по обновлению в вашем вопросе

import json

my_str = '''
###Some Comment 
    First-tags : 
    {
      "tag1": {
        "tagKey1": "tagValue1",
        "tagKey2": "tagValue2"
      },
      "tag2": {
        "tagKey1": "tagValue1",
        "tagKey2": "tagValue2"
      }
    } 


###2nd Comment 
    Second-tags : 
    {
      "tag1": {
        "tagKey1": "tagValue1",
        "tagKey2": "tagValue2"
      },
      "tag2": {
        "tagKey1": "tagValue1",
        "tagKey2": "tagValue2"
      }
    } 

###Some other Comment 
    someother-tags : 
    {
      "tag1": {
        "tagKey1": "tagValue1",
        "tagKey2": "tagValue2"
      },
      "tag2": {
        "tagKey1": "tagValue1",
        "tagKey2": "tagValue2"
      }
    } 
'''
data = []
bal = 0
start = end = 0
for i,v in enumerate(my_str):
    if v == '{': 
        if bal == 0:
            start = i
        bal+=1
    elif v=='}': 
        bal-=1
        end = i
    if start!=end and bal ==0: # just looking for data in {....}
        new_str = my_str[start:end+1]
        print(new_str)
        my_dict = dict(json.loads(new_str))
        data .append(my_dict)
        start = end = i+1
print(data) # n'joy
[{'tag1': {'tagKey1': 'tagValue1', 'tagKey2': 'tagValue2'}, 'tag2': {'tagKey1': 'tagValue1', 'tagKey2': 'tagValue2'}}, {'tag1': {'tagKey1': 'tagValue1', 'tagKey2': 'tagValue2'}, 'tag2': {'tagKey1': 'tagValue1', 'tagKey2': 'tagValue2'}}, {'tag1': {'tagKey1': 'tagValue1', 'tagKey2': 'tagValue2'}, 'tag2': {'tagKey1': 'tagValue1', 'tagKey2': 'tagValue2'}}]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...