簡體   English   中英

從文件夾讀取n個文件以使用python和龍卷風輸入

[英]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一次可以放入內存。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM