code:
import json
import os
dirname = r"D:\json"
basename = "test_json_comment.json"
fpath = os.path.join(dirname, basename)
with open( fpath , encoding="UTF-8" ) as json_file:
dic = json.load(json_file)
json_file.seek(0,0)
strr = json_file.read()
print("strr:",strr)
dic1 = json.loads(strr)
print("\ndic:",dic)
print("\ndic1",dic1)輸出結果:

json.load 方法用於從文件中讀取並解析JSON數據,然後將其轉換為Python數據結構(字典、列表等)。在代碼中,已經使用了 json.load 來加載數據,並將其存儲在了 dic 變量中,因此不需要再次使用 json_file.read() 來讀取JSON文件的內容。
一旦使用 json.load 方法讀取了JSON文件的內容,文件指針就會在文件的末尾,若沒有json_file.seek(0,0)跳回文件的一開頭,再次調用 json_file.read() 將返回空字符串,因為已經到達了文件的末尾。
json.load(json_file) 跟
json.loads(strr)
參數的型別不一樣:

json_file.seek(0,0)
#第一個0表示文件之開頭
#第二個0表示偏移0

推薦hahow線上學習python: https://igrape.net/30afN

![Python: matplotlib繪製出的圖表如何插入背景圖? img = plt.imread(‘background_image.png’) ; ax.imshow(img, extent=[0, 10, -1.2, 1.2], aspect=’auto’, alpha=0.5) Python: matplotlib繪製出的圖表如何插入背景圖? img = plt.imread(‘background_image.png’) ; ax.imshow(img, extent=[0, 10, -1.2, 1.2], aspect=’auto’, alpha=0.5)](https://i0.wp.com/savingking.com.tw/wp-content/uploads/2023/02/20230216183536_29.png?quality=90&zoom=2&ssl=1&resize=350%2C233)
![Python 如何用pandas.Series.nsmallest() 找到n個與target差距最小的index?再從中找到距離idxmax最近的index?避免誤抓sidelobes的index? targetIdx = (serMean-target_value).abs().nsmallest(n).index.tolist() ;Series切片: .loc[標籤名1:標籤名2] (會含標籤名2) ; .iloc[位置1:位置2] (不含位置2) Python 如何用pandas.Series.nsmallest() 找到n個與target差距最小的index?再從中找到距離idxmax最近的index?避免誤抓sidelobes的index? targetIdx = (serMean-target_value).abs().nsmallest(n).index.tolist() ;Series切片: .loc[標籤名1:標籤名2] (會含標籤名2) ; .iloc[位置1:位置2] (不含位置2)](https://i2.wp.com/savingking.com.tw/wp-content/uploads/2023/02/20230222082954_53.png?quality=90&zoom=2&ssl=1&resize=350%2C233)







近期留言