简体   繁体   English

将Excel数据传输到XML

[英]Transferring Excel data to XML

I'm new to handeling XML in python so be easy on me. 我是在python中处理XML的新手,所以对我来说很容易。 i've been trying transfer my excel data to an xml file that looks like so: 我一直试图将我的excel数据传输到看起来像这样的xml文件中:

<?xml version="1.0" encoding="UTF-8"?>
<xml>
    <shelter>
        <adress>..</adress>
        <code>...</code>
        <neighborhood>..</neighborhood>
    </shelter>
    <shelter>
        <adress>...</adress>
        <code>...</code>
        <neighborhood>...</neighborhood>
    </shelter>
</xml>

my excel spread sheet looks like so: 我的Excel电子表格看起来像这样:

在此处输入图片说明

Rather simple right? 相当简单吧? I tried a couple of methodes on excel also i tried to write a script to do but cant seem to make it work. 我在excel上尝试了几种方法,也尝试编写脚本来执行,但似乎无法使其正常工作。 Any ideas? 有任何想法吗? Thanks a lot! 非常感谢!

If this is just a one-time transformation you can use the CONCATENATE function to create the content of your XML. 如果这只是一次转换,则可以使用CONCATENATE函数创建XML的内容。 Put this formula in the D column for all rows: 将此公式放在所有行的D列中:

=CONCATENATE("<shelter><adress>",A2,"</adress><code>",B2,"</code><neighborhood>",C2,"</neighborhood></shelter>")

Then copy the text to a new file, add the appropriate XML tags on the first and last line and you are done. 然后将文本复制到新文件,在第一行和最后一行添加适当的XML标记,即可完成操作。

If you need to do this in Python, save the file as CSV such that you have something like (note that the header line is removed from this file): 如果您需要在Python中执行此操作,请将文件另存为CSV,这样您就可以得到类似的内容(请注意,该文件的标题行已删除):

adress1,1,n1
adress2,2,n1
adress3,3,n1 

Then you can use the following Python script to get the desired output: 然后,您可以使用以下Python脚本获取所需的输出:

print '<?xml version="1.0" encoding="UTF-8"?>'
with open('test.csv','r') as f:
    for x in f:
        splitted = x.split(',')
        print """
  <shelter>
    <adress>{0}</address>
    <code>{1}</code>
    <neighborhood>{2}</neighborhood>
  </shelter>""".format(x[0],x[1],x[2])
print '</xml>'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM