gpt4 book ai didi

Python 字典 : adding a value in a key already made without losing the original value

转载 作者:太空宇宙 更新时间:2023-11-04 08:17:15 24 4
gpt4 key购买 nike

我正在尝试在字典中添加一个 xml 文件。现在我想做的是在不丢失值的情况下将值更新到字典中。这样做的原因是我想对 xml 文件进行分组以显示标记作者所做的所有更改。

这是 xml 文件的示例。

<log> 
<logentry
revision="33185">
<author>glv</author>
<date>2012-08-06T21:01:52.494219Z</date>
<paths>

<path
kind="file"
action="M">/branches/Patch_4_2_0_Branch/text.xml</path>

<path
kind="dir"
action="M">/branches/Patch_4_2_0_Branch</path>

</paths>
<msg>PATCH_BRANCH:N/A
BUG_NUMBER:N/A
FEATURE_AFFECTED:N/A
OVERVIEW:N/A
Adding the SVN log size requirement to the branch
</msg>
</logentry>
....
</log>

这是我用 Python 编写的代码。

from xml.dom import minidom 
import smtplib
from email.mime.text import MIMEText
from datetime import datetime

def xml_data ():

f = open('C:\opidea_2.xml', 'r')

data = f.read()

f.close()

dom = minidom.parseString(data)

ic = (dom.getElementsByTagName('logentry'))

dom = None
content = ''
author = {}
for num in ic:

authors = num.getElementsByTagName('author')

if len(authors) > 0:

name = authors[0].firstChild.nodeValue

author.update({'author': str(name)})

datef = []
xmlDate = num.getElementsByTagName('date')[0].firstChild.nodeValue
datef = [Good_Time]
path_change = []
paths = [x for x in num.getElementsByTagName("paths")[0].childNodes if isinstance(x, minidom.Element)]

for path in paths:

x = path.childNodes[0].nodeValue

if str(path.getAttribute("kind")) == 'dir':

path_change ='Directory location: ' + [str(x)]
else:
path_change ='Filename: ' + [str(x)]

xmlMsgf = []
xmlMsg = num.getElementsByTagName('msg')


if xmlMsg !='' and len(xmlMsg) > 0:
xmlMsgc = xmlMsg[0].firstChild.nodeValue
xmlMsgf = [xmlMsgc]


else:
xmlMsgc = "No comment made."
xmlMsgf = [xmlMsgc]

authorentry.push{('author',authorA),('date',datef),('path',path_change),('path',xmlMsgf))}

for k, v in [(authorentry)]:

try:

author[k].append(v)

except KeyError:

author[k] = [v]

author.update({'Date':datef,'Path':path_change,'msg':str(xmlMsgf)})
print author

if __name__ == "__main__":
xml_data ()

现在我想要的最终结果是这样的

Key 1 = glv , date1 ,path 1a path 1b , msg1 , date2 , path 2 , msg 2 , ...

key 2 = jsm, date1, path 1, msg 1.

我想将 author.update({'Date':datef,'Path':path_change,'msg':str(xmlMsgf)}) 分组以列出作者的 xml 文件以附加下一组内容作者做的。不太确定该怎么做。任何帮助,将不胜感激。这是我更新的代码,但我现在似乎收到了无效的语法错误:

  line 151
authorentry.push{('author',authorA),('date',datef),('path',path_change),('path',xmlMsgf))}
^
SyntaxError: invalid syntax

最佳答案

你不能在字典中“保留旧值”,每个键都有一个与之关联的值。

不过,您可以在字典中使用聚合值类型,例如,您可以使用列表作为值,并且每次您找到要添加的数据时,将其附加到关联列表中。

data = {}

for k, v in [('a', 1), ('a', 2), ('b', 3)]:
try:
data[k].append(v)
except KeyError:
data[k] = [v] # a list of one element!

collections 模块中有一个方便的构造函数,可以使这更容易:

import collections

data = collections.defaultdict(list)
for k, v in [('a', 1), ('a', 2), ('b', 3)]:
data[k].append(v)

关于Python 字典 : adding a value in a key already made without losing the original value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12203419/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com