gpt4 book ai didi

python - HDI : write large string xml into file (python xml. dom.minidom)

转载 作者:太空宇宙 更新时间:2023-11-03 19:29:30 25 4
gpt4 key购买 nike

我目前正在使用 xml.dom.minidom 构建大型 xml 文件,然后通过 toprettyxml 将它们写入文件。有没有办法将 xml 流式传输到文档,因为我遇到了内存错误。

def run(self):
while True:
domain = self.queue.get()
try:
conn = boto.connect_sdb(awsa, awss)
sdbdomain = conn.get_domain(domain)
s3conn = boto.connect_s3(awsa, awss)
archbucket = s3conn.get_bucket("simpledbbu")
doc = None
doc = Document()
root = doc.createElement("items")
doc.appendChild(root)
countermax = 0
counter = 0
for item in sdbdomain:
node = doc.createElement("item")
node.setAttribute("itemName", item.name)
for k,v in item.items():
if not isinstance(v, basestring):
i = 0
for val in v:
node.setAttribute("{0}::{1}".format(k,i),val)
i += 1
else:
node.setAttribute(k,v)
root.appendChild(node)
k = Key(archbucket)
k.key = "{0}/{1}.xml".format(datetime.date.today().strftime("%Y%m%d"),sdbdomain.name)
#x = doc.toprettyxml(indent=" ")
f = open(domain + ".xml", "w")
f.truncate()
f.write(doc.toprettyxml(indent=" "))
f.close()
#k.content_type.encode('ascii')
k.set_contents_from_filename(f.name)
os.remove(os.path.join(os.getcwd(),f.name))
except:
print "failed to load domain: {0}".format(domain)
print formatExceptionInfo()
finally:
self.queue.task_done()

最佳答案

building large xml files with xml.dom.minidom and then writing them out to file via the toprettyxml.

如果内存不足,您可能应该停止这样做。

您可以通过简单的字符串操作构建 XML。

with open(domain + ".xml", "w") as  f:
f.write( "<?xml..." )
f.write( "<items>" )
for item in sdbdomain:
buffer= []
for k,v in item.items():
if not isinstance(v, basestring):
for i, val in enumerate(v):
txt= '{0}::{1}="{2}"'.format(k,i,val)
else:
txt= '{0}="{1}"'.format(k,v)
buffer.append( txt )
f.write( " <item {0}/>\n".format( " ".join(buffer) ))
f.write( "</items>" )
k= ................
k.set_contents_from_filename(f.name)

类似的东西应该允许您将 XML 写入临时文件,而无需在内存中创建大型 DOM 对象。

关于python - HDI : write large string xml into file (python xml. dom.minidom),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6670934/

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