因此,如果输入为txt文件,我们必须创建一个XML树或任何层次结构,以便于解析。然后找到最后一位CEO的最后一位员工
txt给出了一个公司的结构,其中列的顺序如下:名称、工资、雇主。 那些以“无人”为雇主的人是公司的首席执行官。 有雇主名称的人在所述雇主下工作。 txt的外观如下所示:
Vineel Phatak, 520, NOBODY
Ajay Joshi, 250, Vineel Phatak
Abhishek Chauhan, 120, Ajay Joshi
Jayesh Godse, 500, NOBODY
Vijaya Mundada, 60, Abhishek Chauhan
Shital Tuteja, 45, Jayesh Godse
Rajan Gawli, 700, Vineel Phatak
Zeba Khan, 300, Jayesh Godse
Chaitali Sood, 100, Zeba Khan
Sheila Rodrigues, 35, Vineel Phatak
有鉴于此,我们必须完成这样的任务:
Company
->Vineel Phatak
-->Ajay Joshi
--->Abhishek Chauhan
---->Vijaya Mundada
-->Rajan Gawli
-->Sheila Rodrigues
->Jayesh Godse
-->Shital Tuteja
-->Zeba Khan
--->Chaitali Sood
XML格式:
<company>
<Vineel Phatak>
<Ajay Joshi>
<Abhishek Chauhan>
<Vijaya Mundada />
</Abhishek Chauhan>
</Ajay Joshi>
<Rajan Gawli />
<Sheila Rodrigues />
</Vineel Phatak>
<Jayesh Godse>
<Shital Tuteja />
<Zeba Khan>
<Chaitali Sood />
</Zeba Khan>
</Jayesh Godse>
</company>
我试着做的是,在创建了一个名为company的元素之后,因为我们需要向根(company)添加子元素,所以我试着生成这些子元素并附加到列表中。然后对列表进行解析并进行比较以获得值
# Find last employee of the last introduced CEO
import xml.etree.ElementTree as ET
# Reading Input
inD = open('input.txt', 'r')
data = inD.readlines()
inD.close()
# Creating an element and saving all subelement to list
all_element = []
company = ET.Element('Company')
ceos = []
for i in data:
t = i.strip().split(',')
if(t[2].strip() == 'NOBODY'):
ceos.append(t[0])
all_element.append(ET.SubElement(company, t[0]))
# company.clear()
# Creating a function to add subelements
def findChilds(name, emp):
global all_element
for i in all_element:
if emp == i.tag:
name = ET.SubElement(i, name)
# If it is CEO hence no emplyer then directly add subelement to company or else add to the previous subelement
for j in data:
t = j.strip().split(',')
if t[2].strip() == 'NOBODY':
e = ET.SubElement(company, t[0])
elif t[2].strip() != 'NOBODY':
findChilds(t[0].strip(), t[2].strip())
ET.dump(company)
结果如下:
<Company><Vineel Phatak><Ajay Joshi /><Rajan Gawli /><Sheila Rodrigues /></Vineel Phatak><Ajay Joshi><Abhishek Chauhan /></Ajay Joshi><Abhishek Chauhan><Vijaya Mundada /></Abhishek Chauhan><Jayesh Godse><Shital Tuteja /><Zeba Khan /></Jayesh Godse><Vijaya Mundada /><Shital Tuteja /><Rajan Gawli /><Zeba Khan><Chaitali Sood /></Zeba Khan><Chaitali Sood /><Sheila Rodrigues /><Vineel Phatak /><Jayesh Godse /></Company>
你可以看到这并不完全正确。 删除元素(第18行)也不起作用,因为它拒绝添加除CEO以外的子元素
因此,最后,我们需要创建此层次结构,然后打印出最后一位CEO的最后一位员工的姓名,在本例中为:
前任首席执行官:贾耶什·戈泽
首席执行官的最后一名员工(直接或间接,最后从输入中介绍):Chaitali Sood
Output:
Chaitali Sood
首席执行官的人数及其子女和孙子孙女的人数也不确定,姓名也不确定。
我是ElementTree的新手,所以可能有一些预定义的函数我可能不知道,所以请原谅我的无知。 非常感谢您的见解和建议。提前谢谢
在列出我的示例之前,有一条关于xml结构的注释:在创建xml结构时,最好使用“对象类”作为元素的标记,并将其“属性”(如名称和薪水)存储为xml属性:
<employee name="Vineel Phatak" salary="520"/>
而不是:
<Vineel Phatak/>
这将使解析变得更容易,并为扩展格式提供更大的灵活性
我的例子
您的问题的示例实现:
我把练习分为以下几个部分:
结果xml:
相关问题 更多 >
编程相关推荐