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

Reading n number of files from a folder for input using python and 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 名奇数学生的列表。

谢谢

这将遍历目录中的所有 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 一次能装进内存。