试图将文本文件内容导入python中的XML文件中的新标签

我花了几个星期的时间来研究这个问题,但是我总是不太清楚自己需要什么。

我是Python的新手,因为有人向我推荐这是快速处理XML文件的好语言。 但是我正在为此苦苦挣扎,特别是对于这个任务。

问题是这个。

我有一个Excel创build的XML文件。 它有一个树形结构,看起来像这样:

<root> ... <Row> <Entry_No>52</Entry_No> <Waterfall_Name>Switzer_Falls</Waterfall_Name> <Continent__Super_Region>North America</Continent__Super_Region> <Country>USA</Country> <State__Province>California</State__Province> ... <scenic_rating>2</scenic_rating> <difficulty_rating>3.5</difficulty_rating> <distance>4.6 miles round trip (to base of main drop)</distance> <time_commitment>3.5 hours (to base of main drop)</time_commitment> <GPS_Coordinates>34.25828, -118.15474</GPS_Coordinates> ... <File_directory>./waterfall_writeups/52_Switzer_Falls/</File_directory> <Introduction>introduction-switzer-falls.html</Introduction> <Directions>directions-switzer-falls.html</Directions> ... <Post_Slug>california-switzer-falls.html</Post_Slug> </Row> ... </root> 

我想添加新的标签,填充我指向这样的文件的内容:

 <root> ... <Row> <Entry_No>52</Entry_No> <Waterfall_Name>Switzer_Falls</Waterfall_Name> <Continent__Super_Region>North America</Continent__Super_Region> <Country>USA</Country> <State__Province>California</State__Province> ... <scenic_rating>2</scenic_rating> <difficulty_rating>3.5</difficulty_rating> <distance>4.6 miles round trip (to base of main drop)</distance> <time_commitment>3.5 hours (to base of main drop)</time_commitment> <GPS_Coordinates>34.25828, -118.15474</GPS_Coordinates> ... <File_directory>./waterfall_writeups/52_Switzer_Falls/</File_directory> <Introduction>introduction-switzer-falls.html</Introduction> <Directions>directions-switzer-falls.html</Directions> ... <Post_Slug>california-switzer-falls.html</Post_Slug> *<Introduction_Body>a bunch of text taken directly from "introduction-switzer-falls.html" talking about the waterfall complete with links and photo tags</Introduction_Body> <Directions_Body>a bunch of text taken directly from "directions-switzer-falls.html" talking about how to drive to the waterfall complete with links and photo tags</Directions_Body>* </Row> ... </root> 

我试图做到这一点,因为我不能直接把正文文本放入Excel的单元格,因为它的每个单元格的字符数限制(否则,我会有一次我想要的XML)。 所以我必须以某种方式脚本来做到这一点。

在标签“File_directory”的上面示例中,我将文本的主体文本以特定的文件结构暗示在文件中)。 我正在考虑使用这个文件指针来打开XML文件调用的文件,然后将相关的Row标签转储到一个新的标签(这些是上面示例中显示的“Body”标签)中所需的内容。

因为我被告知Python(使用elementTree)是这样做的语言(和库),所以我给了它一个镜头,但我想我不能让我的头围着正确的方式来思考这个问题。

任何人都可以build议最简单和最直观的方式(像我这样的Python新手)做到这一点?

谢谢,约翰尼

因为我是一个Python新手,所有我必须显示我的努力是下面的代码…

 try: import xml.etree.cElementTree as ET except ImportError: import xml.etree.ElementTree as ET import os data_file = 'WoW Database for WP 2016-09-19.xml' tree = ET.ElementTree(file=data_file) root = tree.getroot() 

接下来是我感到困惑的部分。 我恐怕到目前为止所有的东西都是打印出来的东西(从网上的东西看),而不是其他的东西(比如如何理解我的元素),甚至不知道如何应用下面提供的解决scheme在这个导入的树的上下文中。 所以我必须显示的是下面的代码…

 for element in root: print("PARENT: ", element.tag, element.attrib, element.text) for all_tags in element.findall(".//"): print("CHILD: ", all_tags.tag, all_tags.attrib) if all_tags.text: print(all_tags.text, all_tags.tail) 

我可以获得特定的Python语法和代码,如何在本文顶部描述的所需方式编写一个新的XML文件?

谢谢,约翰尼

编辑:更多XML-ish代码示例

XML是从“根”元素开始的树或层次结构。 在根元素下面将是通常对应于主要对象或想法的顶层元素。 每个元素通常都有以某种方式“描述”元素的子元素。 如果这个组织不清楚,我会强烈build议在W3学校对XML进行更多的研究。

像elementTree这样的库可以帮助导航该层次结构。 你可以在你发布的代码中看到这个。 首先你有一个循环:

 for element in root: # 'for' in Python really means 'for each' 

这导航到文档中的每个顶级元素。 在上面发布的XML的情况下,是顶层元素。 下面的所有标签都是子元素。

以下将您的一些代码与Elements和ElementTree中的示例结合在一起。 这是未经testing的,但代表了我会尝试第一手的信息。

 try: import xml.etree.cElementTree as ET except ImportError: import xml.etree.ElementTree as ET import os data_file = 'WoW Database for WP 2016-09-19.xml' tree = ET.ElementTree(file=data_file) root = tree.getroot() for element in root: # 'for' in Python really means 'for each' directory = element.find('file_directory').text introduction = element.find('Introduction').text directions = element.find('Directions').text # I don't know what tag to use in the html file. Assuming body. # this gets text from file and creates Introduction sub-element with text intro_tree = ET.ElementTree(directory+introduction) intro_text = intro_tree.find('body').text intro = SubElement(element,'Introduction') intro.text = intro_text # Do the same for Directions . . . # After the loop, write the file back with new elements added tree.write(data_file) 

这应该给你一个正确的方向很好的推动。 如果不是,请再次评论,我会再细化。