code:
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 20 14:30:55 2023
@author: SavingKing
"""
sample_dict = {
"name": "Alice",
"age": 30,
"city": "Wonderland"
}
import json
# 直接将字典序列化到文件
with open("output_with_dump.json", "w") as file:
json.dump(sample_dict, file, indent=4)
# 先将字典序列化为字符串,然后写入文件
json_str = json.dumps(sample_dict, indent=4)
with open("output_with_dumps.json", "w") as file:
file.write(json_str)code & 輸出結果:

两个文件 output_with_dump.json 和 output_with_dumps.json 将包含相同的JSON内容,但是生成这些文件的过程不同。json.dump() 是一步到位的方法,直接将对象写入文件;而json.dumps() 则是一个两步的过程,先创建字符串,然后将其写入文件。在结果上,两者是等效的:

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




![Python 進階技巧:海象運算子 (Walrus Operator) 實戰教學 [w_clean for w in words if (w_clean:=w.lower().strip()) and w_clean not in STOPWORDS] Python 進階技巧:海象運算子 (Walrus Operator) 實戰教學 [w_clean for w in words if (w_clean:=w.lower().strip()) and w_clean not in STOPWORDS]](https://i2.wp.com/savingking.com.tw/wp-content/uploads/2026/02/20260210083748_0_a7d9bf.png?quality=90&zoom=2&ssl=1&resize=350%2C233)



![Python爬蟲:BeautifulSoup的 .find_all() 與 .find() 與 .select(‘標籤名[屬性名1=”屬性值1″][屬性名2=”屬性值2″]’) ; from bs4 import BeautifulSoup ; Live Server(可以預覽HTML的VS Code套件) Python爬蟲:BeautifulSoup的 .find_all() 與 .find() 與 .select(‘標籤名[屬性名1=”屬性值1″][屬性名2=”屬性值2″]’) ; from bs4 import BeautifulSoup ; Live Server(可以預覽HTML的VS Code套件)](https://i2.wp.com/savingking.com.tw/wp-content/uploads/2025/03/20250330190318_0_925655.jpg?quality=90&zoom=2&ssl=1&resize=350%2C233)

近期留言