[英]Python: Write XML Contents to a file using lxml library
I have a Python function that writes xml string to a file.我有一个 Python function 将 xml 字符串写入文件。 The code is as follows:代码如下:
My file_contents is a string as follows:我的file_contents是一个字符串如下:
<A>Some Text</A>
<B>Some other Text</B>
I would like to enclose this string inside and prepend the xml header: to the file.我想将这个字符串包含在里面并将 xml header: 添加到文件中。 I can't seem to figure this out.我似乎无法弄清楚这一点。
from lxml import etree
def create_or_overwrite_file(file_name, file_contents):
try:
print('file contents to be written: %s' % file_contents)
'''
#root = etree.fromstring(file_contents) #this line definitely fails because file_contents is NOT a valid XML
#print('root: %s' % str(root))
#newroot = etree.Element('RootElement')
#newroot.insert(0, root)
#print('newroot: %s' % newroot)
#new_file_contents = etree.tostring(newroot)
#print('NewFileContents: %s' % str(new_file_contents))
'''
root = etree.fromstring(file_contents).getroot()
et = etree.ElementTree(root)
with open(str(file_name), "wb") as f:
et.write(f, encoding="utf-8", xml_declaration=True, pretty_print=True)
print('wrote file_contents to %s' % str(file_name))
except Exception as f:
print(str(f))
I can't seem to get this code working.我似乎无法让这段代码工作。 Any help is appreciated.任何帮助表示赞赏。
If you are parsing from a string (as opposed to reading from a file).如果您从字符串中解析(而不是从文件中读取)。 then fromstring
already returns the root.然后fromstring
已经返回根。 You do not need the .getroot()
call:您不需要.getroot()
调用:
root = etree.fromstring(file_contents)
With that change, your code works for me.有了这个改变,你的代码对我有用。
Now, it's not clear why you are doing this.现在,尚不清楚您为什么要这样做。 If file_contents
is already XML, as it must be for this to work, then why not just open(file_name).write(file_contents)
?如果file_contents
已经是 XML,因为它必须是这样才能工作,那么为什么不直接open(file_name).write(file_contents)
呢? Why decode the XML and then re-encode it again?为什么解码 XML 然后重新编码呢?
Followup跟进
If all you want is a wrapper, then this would work:如果你想要的只是一个包装器,那么这会起作用:
def create_or_overwrite_file(file_name, file_contents):
f = open(file_name,'w')
print( '<?xml version="1.0"?>\n<root>', file=f )
f.write(file_contents)
print( '</root>', file=f )
It's probably more efficient not to involve lxml
.不涉及lxml
可能更有效。 Where it makes more sense is up to you.哪里更有意义取决于您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.