【发布时间】:2019-10-08 13:48:49
【问题描述】:
我在一个带有点的列表中有一些项目,子点和子子点需要将它们全部传递到父子层次结构中的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."
]
【问题讨论】:
-
除非您创建一个具有属性(= 您的子点)的类并将该类的实例解析为 json,否则我认为您将不得不编写自己的 list 到 json 的方法解释 - 仅基于在上面的给定列表中,除了“数字是主要对象”和“数字之间是不同样式的子项和子项”之外,您没有给出任何结构。对于 json 解析,您需要某种 python 也可以识别并且不是基于理论的结构。
标签: python json python-3.x text-processing