[英]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.