我正在使用以下代碼從 xml 讀取資料并以 stata (.dta) 格式寫入資料。對于每個 XML,我還想將檔案名添加到輸出中。您能否建議對我的代碼進行任何更改,以便我可以在輸出中包含檔案名?
financials = {"id": ["Header/id/text()"]} # there are few more variables in the list
files = glob.glob(path "/*.xml")
list = []
for file in files:
tree = etree.parse(file)
root = tree.getroot()
# building list per xml
s_temp = []
for item in financials:
s_temp.append(choose_xpath(root, financials[item]))
list.append(s_temp)
# Building data frame from the collected data from all the xmls
s_df = pd.DataFrame(list, columns=list(financials.keys()))
s_df.to_stata("List.dta")
uj5u.com熱心網友回復:
glob回傳檔案路徑串列。使用os.path.basename只會獲取檔案名。然后用它開始你的清單:
from os.path import basename
# building list per xml
s_temp = [basename(file)] # filename
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/459318.html
上一篇:如何在加載CSS時運行影片
