我目前正在完成一个程序,该程序应该解析一个xml文件,然后设置它,然后通过电子邮件发送它。我几乎完成了它,但我似乎陷入了困境,需要一些帮助。我试图从完整的字典中获取路径属性"dir"one_answers"file",但我遇到了一个错误。这是我目前正在处理的部分代码。
更新:我添加了一条dictionary,告诉我这是目录更改还是文件名更改旁边的列表
def formatChangesByAuthor(changesByAuthor):
content = ''
for author, changes in sorted(changesByAuthor.iteritems()):
authorName = get_author_name( author );
content += ' ***** Changes By '+authorName+'*****nn'
for change in changes:
rawDate = change['date']
dateRepresentation = rawDate.strftime("%m/%d/%Y %I:%M:%S %p")
content += ' Date: '+ dateRepresentation+'nn'
path_change = change['paths']
path_attribute= change['Attribute_changes']
finalizedPathInformation = []
for single_change in path_change:
for single_output in path_attribute:
finalizedPathInformation.append(single_output+single_change)
content +=" " + str(finalizedPathInformation) + "n"
我希望开始工作的是,当路径中有多条路径时,它会进行
filename:test.xml
目录位置:/Testforlder/
出于某种原因,它显示了一个内存错误。我想知道我是否可以把路径部分写得更好。这是我的错误。
content +=" " + str(finalizedPathInformation) + "n" MemoryError
如果有帮助的话,这里是xml文件的一部分。
<log>
<logentry
revision="33185">
<author>glv</author>
<date>2012-08-06T21:01:52.494219Z</date>
<paths>
<path
action="M"
kind="file">/trunk/build.xml</path>
<path
kind="dir"
action="M">/trunk</path>
</paths>
<msg>PATCH_BRANCH:N/A
BUG_NUMBER:N/A
FEATURE_AFFECTED:N/A
OVERVIEW:N/A
Testing the system log.
</msg>
</log>
现在我在字典中保存了nodeValue,但由于某种原因,我无法在nodeValue中获取属性,以查看它是否有"dir"或"file"属性。我很确定我在哪里做错了什么。如有任何建议或帮助,我们将不胜感激:)
显然,您的path_change
是一个列表。尝试path_change[0].getAttribute('kind')
。
我还建议添加调试输出(在调用getAttribute()之前添加print path_change
),以了解有关数据结构的更多信息。
当然,如果有一个列表,您可能希望处理列表中的所有元素,而不仅仅是第一个。一种方法是:
...
for single_change in path_change:
kind = str(single_change.getAttribute("kind"))
if kind == 'dir':
content += ' Directory Location: '
elif kind == 'file':
content += ' Filename: '
else:
raise SomeException("kind is neither dir nor file", kind, single_change)
content += (str(single_change).
replace("u'"," n").
replace("[","").
replace("',","").
replace("']", "n ") + "n")
...