good afternoon

tell me how to parse a text file on python .. file of the form:

первое 3 текст | текст | текст текст| текст| текст текст| текст| текст второе 5 текст| текст| текст текст| текст | текст текст| текст| текст текст| текст | текст текст| текст| текст 

it is necessary to continue the parsing on the second line, with a number, i.e. it should indicate that after it you need to parse another n-elements, after which the cycle should re-parse 1 element, see how many lines you need to go around further and so on until the end ..

at the output you need to get a dictionary where the key is the first line, and the value is a list of the lines "text | text | text"

    2 answers 2

     def get_item(f): key = f.next().strip('\n') n = int(f.next().strip()) lst = [] for i in range(n): lst += f.next().strip('\n').split('|') return key, lst data = {} with open('test.txt') as f: while True: try: key, lst = get_item(f) data[key] = lst next(f) except StopIteration: break print data 

    test.txt:

     item1 2 text|text|text text|text item2 3 txt|txt txt|xxx|txt txt|txt 

    Conclusion:

     {'item2': ['txt', 'txt', 'txt', 'xxx', 'txt', 'txt', 'txt'], 'item1': ['text', 'text', 'text', 'text', 'text']} 
       lines = [line.strip() for line in open('123.txt')] result = {lines[n-1]: lines[n+1: n+1+int(lines[n])] for n in range(len(lines)) if lines[n].isnumeric()}