您还可以将结果更改为将文件名作为键,因为它们是唯一的:
{
"SCAN1.txt": {
"Filesize": 7196,
"Lastmodified": 20191125.121049
},
"SCAN2.txt": {
"Filesize": 3949,
"Lastmodified": 20191125.121017
},
"SCAN3.txt": {
"Filesize": 2766,
"Lastmodified": 20191125.121056
}
}
可实现如下(包括评论):
from collections import defaultdict
from json import dumps
from ast import literal_eval
lst = [
"SCAN1.txt",
"Lastmodified:20191125.121049",
"Filesize:7196",
"SCAN2.txt",
"Lastmodified:20191125.121017",
"Filesize:3949",
"SCAN3.txt",
"Lastmodified:20191125.121056",
"Filesize:2766",
]
def group_file_documents(lst, prefix="SCAN"):
# Use defaultdict of dicts to represent final JSON structure
# Also can be serialized like normal dictionaries
result = defaultdict(dict)
current_file = None
for item in lst:
# Update current file name if starts with prefix
if item.startswith(prefix):
current_file = item
continue
# Ensure current file name is present
if current_file:
# Split key.values and strip whitespace, just in case
key, value = map(str.strip, item.split(":"))
# Convert to actual int/float value
result[current_file][key] = literal_eval(value)
return result
# Print serialized JSON file with sorted keys and indents of 4 spaces
print(dumps(group_file_documents(lst), sort_keys=True, indent=4))