将txt解析为块 [英] Parse txt to blocks
本文介绍了将txt解析为块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个txt文件,该文件具有以下结构
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
并且我需要在字典列表中解析它(str:str(即使它是int类型或日期:将其转换为字符串)).即使用start
end
将其拆分为块,然后将其拆分为=
符号. start
end
之间的行数可以不同. d
但是无法实现.我尝试过这样的事情:
and i need to parse it in a list of dictionaries (str : str (even it is int type or date: convert it to string)). i.e. split it on block with start
end
, and after that split it on =
symbol. The amount of lines between start
end
can be different. D
But a can't realize it. I tried something like this:
d ={}
arr = []
ind = 0
for line in plines:
ind = ind + 1
if 'startpayment' in line:
print('ind = ' + str(ind))
for i in range(ind, len(plines)):
print(i)
key, value = plines[i].strip().split('=')
if type(value) == 'str':
d[key] = str(value)
elif type(value) == 'int':
d[key] = int(value)
arr.append(d)
if 'endpayment' in line:
break
有人可以帮我吗?谢谢
推荐答案
您还可以尝试以下方法:
You could also try something like this:
from itertools import takewhile
with open('data.txt') as in_file:
items = [line.strip() for line in in_file.read().split()]
# ['start', 'id=1', 'date=21.05.2018', 'summ=500', 'end', 'start', 'id=7', 'date=23.05.2018', 'summ=500', 'owner=guest']
pos = [i for i, item in enumerate(items) if item == 'start']
# [0, 5]
blocks = [list(takewhile(lambda x: x != 'end', items[i+1:])) for i in pos]
# [['id=1', 'date=21.05.2018', 'summ=500'], ['id=7', 'date=23.05.2018', 'summ=500', 'owner=guest']]
print([dict(x.split('=') for x in block) for block in blocks])
哪些输出:
[{'id': '1', 'date': '21.05.2018', 'summ': '500'}, {'id': '7', 'date': '23.05.2018', 'summ': '500', 'owner': 'guest'}]
这篇关于将txt解析为块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文