我想從使用json庫的python腳本讀取json文件。一些谷歌搜索後,我發現了以下代碼:從python讀取json文件
with open(json_folder+json) as json_file:
json_data = json.loads(json_file)
print(json_data)
其中json_folder + json json文件的路徑和名稱。我得到以下錯誤str對象沒有屬性加載。
我想從使用json庫的python腳本讀取json文件。一些谷歌搜索後,我發現了以下代碼:從python讀取json文件
with open(json_folder+json) as json_file:
json_data = json.loads(json_file)
print(json_data)
其中json_folder + json json文件的路徑和名稱。我得到以下錯誤str對象沒有屬性加載。
該代碼使用json
作爲變量名稱。它會隱藏您導入的模塊引用。爲變量使用不同的名稱。
除此之外,代碼傳遞文件對象,而json.loads
接受一個字符串。
傳遞一個文件的內容:
json_data = json.loads(json_file.read())
,或者使用json.load
它接受類文件對象。
json_data = json.load(json_file)
import json
f = open("fileToOpen.json" , "rb")
jsonObject = json.load(f)
f.close()
它應該看來你是相當複雜的方式做英寸
嘗試這樣的: -
json_data=open(json_file)
data = json.load(json_data)
json_data.close()
我有這樣的....
import urllib2
link_json = "\\link-were\\"
link_open = urllib2.urlopen(link_json) ## Open and Return page.
link_read = link_open.read() ## Read contains of page.
json = eval(link_read)[0] ## Transform the string of read in link_read and return the primary dictionary ex: [{dict} <- return this] <- remove this
print(json['helloKey'])
Hello World
不知道這是如何相關。 – dantiston 2014-11-21 21:36:04
考慮的路徑,你的JSON文件設置爲可變json_file
:
import json
with open(json_file, "rb") as f:
json_data = json.load(f)
print json_data
如果json是一個帶有文件名的字符串,你想通過調用字符串上的加載來達到什麼目的? – 2014-10-03 11:36:40