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
]*>.*?底下插入一個圖檔.*?</w:p>’, flags = re.DOTALL) ; new_xml, n = pattern.subn(”, xml, count=1)' title='Python正則替換:全面掌握 re.sub 與 re.subn 的差異與實戰 #substitute(替換); . 預設匹配「除\n以外的任意單一字元」; pattern = re.compile(r'<w:p[^>]*>.*?底下插入一個圖檔.*?</w:p>’, flags = re.DOTALL) ; new_xml, n = pattern.subn(”, xml, count=1)' loading='lazy' width=350 height=233 />


![Python: pandas.Series如何只保留str,去除重複值?#isinstance(x:Any, str) -> bool #.drop_duplicates() #Series.apply( function )逐元素應用function運算 #DataFrame.apply( function )逐Series應用function運算 .drop_duplicates() 跟.unique()有何差別? df.drop_duplicates() 等效於 df[~df.duplicated()] Python: pandas.Series如何只保留str,去除重複值?#isinstance(x:Any, str) -> bool #.drop_duplicates() #Series.apply( function )逐元素應用function運算 #DataFrame.apply( function )逐Series應用function運算 .drop_duplicates() 跟.unique()有何差別? df.drop_duplicates() 等效於 df[~df.duplicated()]](https://i0.wp.com/savingking.com.tw/wp-content/uploads/2024/11/20241123194900_0_5218de.png?quality=90&zoom=2&ssl=1&resize=350%2C233)

![Python: 如何在pandas.read_csv() or pandas.read_excel() 中使用keep_default_na =False , na_values =[] 保留文件中的原始“NA”? Python: 如何在pandas.read_csv() or pandas.read_excel() 中使用keep_default_na =False , na_values =[] 保留文件中的原始“NA”?](https://i2.wp.com/savingking.com.tw/wp-content/uploads/2024/05/20240530215737_0.png?quality=90&zoom=2&ssl=1&resize=350%2C233)



近期留言