Parse txt to blocks

前端 未结 5 1228
时光说笑
时光说笑 2021-01-26 17:56

i have a txt file, which has the following structure

start
id=1
date=21.05.2018
summ=500
end

start
id=7
date=23.05.2018
summ=500
owner=guest
end
5条回答
  •  佛祖请我去吃肉
    2021-01-26 18:15

    Easiest algorithm I could think of, if I get your question right.

    d ={}
    arr = []
    
    for line in plines:
      if line == 'start':
        continue
      elif line =='end':
        arr.append(d)
        continue
      else:
        list_key_value = line.split('=')    
        d[list_key_value[0]] = int(list_key_value[1]) if 
        type(list_key_value[1]) == 'int' else str(list_key_value[1])
    print (arr)
    

    Output: [{'id': '7', 'date': '23.05.2018', 'summ': '500', 'owner': 'guest'}, {'id': '7', 'date': '23.05.2018', 'summ': '500', 'owner': 'guest'}]

提交回复
热议问题