我的驅動器上有各種 JSON 檔案,我想回圈處理它們,但是如何回圈讀取它們?
基本上我有一個串列,其中包含所有檔案名,所有檔案也在一個檔案夾中。
目標是在回圈中創建列出 json 檔案的串列。
TestList = ["cats", "dogs"]
for i in TestList:
with open ("{i}.json") as {i}_file:
print({i}_file)
不幸的是,無論我如何嘗試,我都會遇到語法錯誤。
非常感謝您的支持!
uj5u.com熱心網友回復:
采用:
TestList = ["cats", "dogs"]
for i in TestList:
with open(f"{i}.json") as fp:
print(fp.read())
首先,如果您使用"{i}.json",請添加前綴f以將此字串定義為f-strings。
然后你的變數{i}_file不能被動態評估來創建變數cats_file和dogs_file. 您必須使用靜態名稱。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/438098.html
上一篇:執行時Lua中的Load函式問題
