简体   繁体   English

从文件夹读取n个文件以使用python和龙卷风输入

[英]Reading n number of files from a folder for input using python and tornado

Hi I am trying to add values to my postgres database and the values to be inserted are in xml format in multiple files (since multiple data values). 嗨,我正在尝试将值添加到我的postgres数据库中,并且要插入的值是xml格式的多个文件(因为有多个数据值)。 My script accepts only XML as input. 我的脚本仅接受XML作为输入。 Is there a way so that I can read data from all the files in that particular folder and send it to my database in the same XML format. 有没有一种方法可以使我从该特定文件夹中的所有文件中读取数据,并将其以相同的XML格式发送到我的数据库。 using python and tornado. 使用python和龙卷风。

Initially i did like this: 最初我确实是这样的:

data = "<xml>the entire xml file content here </xml>"
content = document_create(student_id=req['student_id'], body =data)
self.write(content)

My xml looks like:(test1.xml) 我的xml看起来像:(test1.xml)

<Students >
<name> Alex </name>
<no. of days present>20</no. of days present>
<no. of days absent></no. of days absent>
<subject>
.....
  <total>458</total>
</subject>
<staff>
Martin
</staff>
</Students>

For a list of 100 odd students. 对于100名奇数学生的列表。

Thanks 谢谢

This will iterate over all xml in the directory and open xml files as data, 这将遍历目录中的所有xml,并将xml文件作为数据打开,

import glob
ListofFiles = glob.glob("/<Dir>/*.<xml or file format in folder>")
for files in ListofFiles:
    with open(files,'r') as fl:
        content = fl.read()
        data = "<xml>%s<xml>" % content
        <your code>
        ...

I am assuming your one xml will fit into memory at a time. 我假设您的一个xml一次可以放入内存。

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

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