[英]Grouping contents in a text file in python
我有以下格式的输入文件
CC -----------------------------------------------------------------------
CC
CC hgfsdh kjhsdt kjshdk
CC
CC -----------------------------------------------------------------------
CC Release of 18-Sep-2019
CC -----------------------------------------------------------------------
CC
CC Alex
CC -----------------------------------------------------------------------
CC Copyrighted vvvncbm License
CC -----------------------------------------------------------------------
//
ID 1.1
ED text1
AN text2.
CA text3
CF text4.
CC -!- Some members
CC also on .
CC -!- May be
CC -!- Re
PR PRTSF; C000AS61;
DQ Q6, AZW2_DANRE; Q7, AZW2_DANRE; Q97, AZW2_DONT;
DQ Q8, AZW2_DONT; Q9, AZW2_AZW2_DONT; Q10, AZW2_CAFT;
//
ID 1.2
ED text1
AN text2.
CA text3
CF text4.
CC -!- Some members
CC also on .
CC -!- May be
CC second line
PR PRTSF; DOC00;
DQ Q6, AZW2_DANRE; Q7, AZW2_DANRE; Q97, AZW2_DONT;
DQ Q8, AZW2_DONT; Q9, AZW2_AZW2_DONT; Q10, AZW2_CAFT;
DQ Q15, AZW2_DANRE; Q43, AZW2_DANRE; Q049, AZW2_DONT;
//
我想将此文本文件中的数据分组并将其存储在 json
我试过以下,
import os
import json
from pprint import pprint
def text_to_json(f_input):
location_data = []
if os.path.exists(f_input):
with open(f_input, 'r') as f:
for line in f.readlines()[12:]:
if line.strip() != '//' and line.strip() != '//' and line.strip():
print(line[:-1])
pass
# return json.dumps(data)
if __name__ == '__main__':
f_input = 'input.txt'
text_to_json(f_input)
我跳过了前几行的评论。 if line.strip().= 'DELIMITER' and line.strip():= 'DELIMITER' and line.strip():
,分隔符是//
。 但是,我不确定如何使用\\
并将每个 id 对应的数据分组。
我想使用分隔符对数据进行分组,并以 json 格式存储每个 id 的数据。
{
'1.1' :
{'DQ': {'Q6': AZW2_DANRE, 'Q7': 'AZW2_DANRE', 'Q97': 'AZW2_DONT'
'Q8': 'AZW2_DONT', 'Q9': 'AZW2_AZW2_DONT'; 'Q10': 'AZW2_CAFT'},
'ED': 'text1',
'AN': 'text2.',
'CA': 'text3',
'CF': 'text4.',
'PR': 'PRTSF; C000AS61;',
'CC': ['Some members also on .', 'May be', 'Re']
}
'1.2' :
{'DQ': {'Q6': AZW2_DANRE, 'Q7': 'AZW2_DANRE', 'Q97': 'AZW2_DONT'
'Q8': 'AZW2_DONT', 'Q9': 'AZW2_AZW2_DONT'; 'Q10': 'AZW2_CAFT',
'Q15': 'AZW2_DANRE', 'Q43': 'AZW2_DANRE', 'Q049': 'AZW2_DONT'},
'ED': 'text1',
'AN': 'text2.',
'CA': 'text3',
'CF': 'text4.',
'PR': 'PRTSF; DOC00;',
'CC': ['Some members also on .', 'May be second line']
}
}
我可以通过基于行号存储来创建上述 json。 但是,每个数据集的行号不断变化。 例如,针对DQ
存储的数据在第一个数据集中有 2 行,在第二个数据集中有 3 行。 关于如何进行的任何建议?
我建议采用在 memory、字典和 arrays 中构建所有内容的方法。 在下面的代码中,所有内容都被累积到d
字典中。 然后将 memory 中的数据转储为 JSON object。 看起来你想以不同的方式处理不同类型的行('CC' 行变成一个数组,'DQ' 行变成一个字典,其他行只是存储)。 所以,这就是我将如何处理代码:
import os
import json
from pprint import pprint
def text_to_json(f_input):
location_data = []
if os.path.exists(f_input):
with open(f_input, 'r') as f:
# Accumulate all of the line data in this dictionary
d = {}
# This keeps track of the current ID, like 1.1 or 1.2
current_line_id = ''
for line in f.readlines()[12:]:
if line.strip() != '//' and line.strip() != '//' and line.strip():
# print(line[:-1])
line_type = line[0:2]
line_data = line[5:-1]
if line_type == 'ID':
d[line_data] = dict()
current_line_id = line_data
elif line_type == 'CC':
if line_type not in d[current_line_id]:
d[current_line_id][line_type] = []
d[current_line_id][line_type].append(line_data)
elif line_type == 'DQ':
if line_type not in d[current_line_id]:
d[current_line_id][line_type] = {}
for dq in line_data.split(';'):
dq = dq.strip()
dq_key = dq[0:2]
dq_val = dq[4:]
if dq_key != '':
d[current_line_id][line_type][dq_key] = dq_val
else:
d[current_line_id][line_type] = line_data
pass
print(json.dumps(d, indent=2))
# return json.dumps(data)
if __name__ == '__main__':
f_input = 'input.txt'
text_to_json(f_input)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.