我在列表中有一些带有点,子点和子子点的项目,需要将它们全部传递给父子层次结构中的json。
如果一个点由点,子点或子子点组成,那么我尝试将每个点都列出来。
我的列表显示如下:
lst=["1. content","(a) content","(b) ","(i)","(ii"),"(c)","2.","3.","(A)","(B)","4."]
for ptags in soup.findAll('p'):
lst.append(ptags.get_text())
regex = r"^\([a-z]\)\s.*"
regex1=r"^\([\D]+\)\s.*"
j=0
sub = []
for i in lst:
if sub:
match = re.match(regex, i)
match1=re.match(regex1,i)
if match:
sub.append(i)
elif match1:
sub.append(i)
else:
j=j+1
sub = [i]
Notes[str(j)] = sub
else:
if sub:
Notes[str(j)] = sub
sub = [i]
Notes[str(j)] = i
I need the json hierarchy as output in this way :
"1. content",
"(a) content",
"(b) ",
"(i)",
"(ii"),
"(c)",
"2.",
"3.",
"(A)",
"(B)",
"4."
######################################JSON STRUCTURE
[
{
"1. content": [
"(a) content",
{
"(b) ": [
"(i)",
"(ii)"
]
},
"(c)"
]
},
"2.",
{
"3.": [
"(A)",
"(B)"
]
},
"4."
]
答案 0 :(得分:0)
如果要具有相似的层次结构,则应将数据更改为dict
。因为您的代码中没有包含足够的信息,所以我只添加一个示例,以说明您的数据应该是什么样的:
from json import dumps
lst = [{"1. content": ["(a) content", {"(b) ": ["(i)","(ii)"]},"(c)"]},"2.",{"3.": ["(A)","(B)"]},"4."]
每个层次结构级别都应该是一个字典。对于没有子元素的元素,您可以将它们作为简单的列表元素传递。
您现在可以使用dumps
来获取json字符串:
dumps(lst)