从文件夹中读取n个文件,以便使用python和tornado进行输入

时间:2015-09-29 04:49:47

标签: python xml postgresql tornado

您好我正在尝试为我的postgres数据库添加值,并且要插入的值是xml格式的多个文件(因为多个数据值)。 我的脚本只接受XML作为输入。有没有办法让我可以从该特定文件夹中的所有文件中读取数据,并以相同的XML格式将其发送到我的数据库。 使用python和龙卷风。

最初我确实喜欢这个:

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

我的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>

获得100多名学生的名单。

由于

1 个答案:

答案 0 :(得分:1)

这将迭代目录中的所有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>
        ...

我假设您的一个xml一次适合内存。