我一直在努力解決這個問題,但現在我需要一些幫助。我正在嘗試在 Spyder IDE 中上傳這個 JSON 檔案 (DBL)。我已將 JSON 資料檔案和 Spyder 檔案存盤在同一個映射中以讀取 JSON 檔案,但它不起作用。
我的 Python 代碼:
import json
file = open("dbl")
dbl = json.load(file)
print(dbl)
每次上傳json檔案,和spyder.py檔案在同一個map中,無法識別檔案目錄。
我已將我的 .py 檔案存盤在與 JSON 檔案相同的檔案夾中。
這是錯誤訊息:
FileNotFoundError: [Errno 2] No such file or directory: 'dbl.json'
uj5u.com熱心網友回復:
您將在檔案路徑中使用“.json”。例如:
file = open("dbl.json")
uj5u.com熱心網友回復:
如果 dbl 是 json 檔案的名稱,那么您也應該添加“.json”擴展名。你可以這樣做:
# Opening JSON file
f = open('dbl.json',)
# returns JSON object as
# a dictionary
data = json.load(f)
# Iterating through the json
# list
for i in data:
print(i)
# Closing file
f.close()```
uj5u.com熱心網友回復:
代碼很好,但是,添加檔案擴展名是一種很好的做法。看起來您忘記添加擴展名了。您正在使用相對路徑。建議使用絕對路徑。在這種情況下,將python腳本和db1檔案放在同一目錄下,然后再試一次。
如果您想除錯,只需在腳本頂部添加以下代碼以查看檔案是否存在并相應地修改腳本。
import os;
print(os.listdir())
uj5u.com熱心網友回復:
實際上,該檔案不存在。實際檔案名是 dbl.json.json。
import json
file = open("dbl.json.json")
dbl = json.load(file)
print(dbl)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/333868.html
上一篇:php-將中文文本決議為json
