Tệp import json article_info = { "domain" : "tutswiki", "language" : "python", "date" : "11/09/2020", "topic" : "config file" } myJSON = json.dumps(article_info) with open("tutswiki.json", "w") as jsonfile: jsonfile.write(myJSON) print("Write successful")3 cho tệp import json article_info = { "domain" : "tutswiki", "language" : "python", "date" : "11/09/2020", "topic" : "config file" } myJSON = json.dumps(article_info) with open("tutswiki.json", "w") as jsonfile: jsonfile.write(myJSON) print("Write successful")4 là một tệp lưu trữ thông tin như tham số, cài đặt, cấu hình và tùy chọn của một ứng dụng.
import json article_info = { "domain" : "tutswiki", "language" : "python", "date" : "11/09/2020", "topic" : "config file" } myJSON = json.dumps(article_info) with open("tutswiki.json", "w") as jsonfile: jsonfile.write(myJSON) print("Write successful")5 chỉ đơn giản là các tệp văn bản đơn giản với import json article_info = { "domain" : "tutswiki", "language" : "python", "date" : "11/09/2020", "topic" : "config file" } myJSON = json.dumps(article_info) with open("tutswiki.json", "w") as jsonfile: jsonfile.write(myJSON) print("Write successful")6, import json article_info = { "domain" : "tutswiki", "language" : "python", "date" : "11/09/2020", "topic" : "config file" } myJSON = json.dumps(article_info) with open("tutswiki.json", "w") as jsonfile: jsonfile.write(myJSON) print("Write successful")7, import json article_info = { "domain" : "tutswiki", "language" : "python", "date" : "11/09/2020", "topic" : "config file" } myJSON = json.dumps(article_info) with open("tutswiki.json", "w") as jsonfile: jsonfile.write(myJSON) print("Write successful")8, import json article_info = { "domain" : "tutswiki", "language" : "python", "date" : "11/09/2020", "topic" : "config file" } myJSON = json.dumps(article_info) with open("tutswiki.json", "w") as jsonfile: jsonfile.write(myJSON) print("Write successful")9, Write successful Process finished with exit code 00 Tiện ích mở rộng tệp trong số các phần khác có thể được tạo, xem hoặc chỉnh sửa bằng bất kỳ trình soạn thảo văn bản nào.
Ví dụ: tệp Write successful Process finished with exit code 01 trong ứng dụng Microsoft ASP.NET MVC chứa thông tin cấu hình kiểm soát hoạt động của ứng dụng. Nó có thể dành cho các trang riêng lẻ hoặc toàn bộ ứng dụng.
Dưới đây là một tập tin Write successful Process finished with exit code 01 mẫu.
<configuration> <appSettings> <add key="tutswiki" value="python" /> <add key="article" value="Config file" /> </appSettings> </configuration>Chúng tôi đã đề cập đến import json article_info = { "domain" : "tutswiki", "language" : "python", "date" : "11/09/2020", "topic" : "config file" } myJSON = json.dumps(article_info) with open("tutswiki.json", "w") as jsonfile: jsonfile.write(myJSON) print("Write successful")7, Write successful Process finished with exit code 00 và import json article_info = { "domain" : "tutswiki", "language" : "python", "date" : "11/09/2020", "topic" : "config file" } myJSON = json.dumps(article_info) with open("tutswiki.json", "w") as jsonfile: jsonfile.write(myJSON) print("Write successful")9 trong các bài viết dưới đây, trong bài viết này, chúng tôi sẽ tập trung vào các tệp cấu hình import json article_info = { "domain" : "tutswiki", "language" : "python", "date" : "11/09/2020", "topic" : "config file" } myJSON = json.dumps(article_info) with open("tutswiki.json", "w") as jsonfile: jsonfile.write(myJSON) print("Write successful")8.
- Tệp .ini trong Python
- Tệp .yaml trong Python
- Tệp .xml trong Python
Đọc và ghi dữ liệu cấu hình vào tệp JSON trong Python
Tệp Write successful Process finished with exit code 07 hoặc Write successful Process finished with exit code 08 được sử dụng để lưu trữ và chuyển dữ liệu dưới dạng các cặp Write successful Process finished with exit code 09 hoặc { "domain": "tutswiki", "language": "python", "date": "11/09/2020", "topic": "config file" }0. Bây giờ hãy hiểu các hoạt động đọc và viết vào tệp JSON.
Tạo tệp cấu hình JSON trong Python
Có 2 phương pháp để viết trong tệp JSON.
Sử dụng { "domain": "tutswiki", "language": "python", "date": "11/09/2020", "topic": "config file" }1
{ "domain": "tutswiki", "language": "python", "date": "11/09/2020", "topic": "config file" }1 lấy { "domain": "tutswiki", "language": "python", "date": "11/09/2020", "topic": "config file" }3 làm tham số.
Đầu tiên, chúng ta cần nhập mô -đun { "domain": "tutswiki", "language": "python", "date": "11/09/2020", "topic": "config file" }4. Phương thức { "domain": "tutswiki", "language": "python", "date": "11/09/2020", "topic": "config file" }1 tuần tự hóa (chuyển đổi dữ liệu thành loạt byte) đối tượng Python ({ "domain": "tutswiki", "language": "python", "date": "11/09/2020", "topic": "config file" }6 trong trường hợp này) thành chuỗi được định dạng JSON và phương thức { "domain": "tutswiki", "language": "python", "date": "11/09/2020", "topic": "config file" }7 ghi định dạng chuỗi JSON vào tệp { "domain": "tutswiki", "language": "python", "date": "11/09/2020", "topic": "config file" }8.
import json article_info = { "domain" : "tutswiki", "language" : "python", "date" : "11/09/2020", "topic" : "config file" } myJSON = json.dumps(article_info) with open("tutswiki.json", "w") as jsonfile: jsonfile.write(myJSON) print("Write successful")
Output:
Write successful Process finished with exit code 0Tệp: Tutswiki.json
{ "domain": "tutswiki", "language": "python", "date": "11/09/2020", "topic": "config file" }LƯU Ý: Chế độ W W War tạo tệp trong thư mục làm việc hiện tại nếu nó không tồn tại. “w” mode creates the file in the current working directory if it does not exists.
Sử dụng { "domain": "tutswiki", "language": "python", "date": "11/09/2020", "topic": "config file" }9
Ở đây, không giống như { "domain": "tutswiki", "language": "python", "date": "11/09/2020", "topic": "config file" }1, chúng ta không cần phải tuần tự hóa đối tượng Python thành chuỗi JSON. Thay vào đó, phương thức { "domain": "tutswiki", "language": "python", "date": "11/09/2020", "topic": "config file" }9 trực tiếp lưu trữ đối tượng Python dưới dạng dữ liệu được định dạng JSON vào tệp JSON.
{ "domain": "tutswiki", "language": "python", "date": "11/09/2020", "topic": "config file" }9 lấy { "domain": "tutswiki", "language": "python", "date": "11/09/2020", "topic": "config file" }3 và import json article_info = { "domain" : "tutswiki", "language" : "python", "date" : "11/09/2020", "topic" : "config file" } with open("tutswiki.json", "w") as jsonfile: json.dump(article_info, jsonfile)4 làm tham số.
import json article_info = { "domain" : "tutswiki", "language" : "python", "date" : "11/09/2020", "topic" : "config file" } with open("tutswiki.json", "w") as jsonfile: json.dump(article_info, jsonfile)
Tệp: Tutswiki.json
{ "domain": "tutswiki", "language": "python", "date": "11/09/2020", "topic": "config file" }{ "domain": "tutswiki", "language": "python", "date": "11/09/2020", "topic": "config file" }
LƯU Ý: Chế độ W W War tạo tệp trong thư mục làm việc hiện tại nếu nó không tồn tại.
import json with open("tutswiki.json", "r") as jsonfile: data = json.load(jsonfile) print("Read successful") print(data)
Output:
Read successful { 'domain': 'tutswiki', 'language': 'python', 'date': '11/09/2020', 'topic': 'config file' }Sử dụng { "domain": "tutswiki", "language": "python", "date": "11/09/2020", "topic": "config file" }9 If we want to deserialize a JSON string to a python object directly instead of reading from a file, we use import json article_info = { "domain" : "tutswiki", "language" : "python", "date" : "11/09/2020", "topic" : "config file" } with open("tutswiki.json", "w") as jsonfile: json.dump(article_info, jsonfile)8 method which takes a import json article_info = { "domain" : "tutswiki", "language" : "python", "date" : "11/09/2020", "topic" : "config file" } with open("tutswiki.json", "w") as jsonfile: json.dump(article_info, jsonfile)9 as a parameter. For instance,
import json s = "{\"domain\": \"tutswiki\", \"language\": \"python\"}" data = json.loads(s) print(data)
Output:
{ 'domain': 'tutswiki', 'language': 'python', 'date': '11/09/2020', 'topic': 'config file' } Process finished with exit code 0
Ở đây, không giống như { "domain": "tutswiki", "language": "python", "date": "11/09/2020", "topic": "config file" }1, chúng ta không cần phải tuần tự hóa đối tượng Python thành chuỗi JSON. Thay vào đó, phương thức { "domain": "tutswiki", "language": "python", "date": "11/09/2020", "topic": "config file" }9 trực tiếp lưu trữ đối tượng Python dưới dạng dữ liệu được định dạng JSON vào tệp JSON.
{ "domain": "tutswiki", "language": "python", "date": "11/09/2020", "topic": "config file" }9 lấy { "domain": "tutswiki", "language": "python", "date": "11/09/2020", "topic": "config file" }3 và import json article_info = { "domain" : "tutswiki", "language" : "python", "date" : "11/09/2020", "topic" : "config file" } with open("tutswiki.json", "w") as jsonfile: json.dump(article_info, jsonfile)4 làm tham số.
import json article_info = { "domain" : "tutswiki", "language" : "python", "date" : "11/09/2020", "topic" : "config file" } myJSON = json.dumps(article_info) with open("tutswiki.json", "w") as jsonfile: jsonfile.write(myJSON) print("Write successful")0
Output:
import json article_info = { "domain" : "tutswiki", "language" : "python", "date" : "11/09/2020", "topic" : "config file" } myJSON = json.dumps(article_info) with open("tutswiki.json", "w") as jsonfile: jsonfile.write(myJSON) print("Write successful")1Tệp: Tutswiki.json
import json article_info = { "domain" : "tutswiki", "language" : "python", "date" : "11/09/2020", "topic" : "config file" } myJSON = json.dumps(article_info) with open("tutswiki.json", "w") as jsonfile: jsonfile.write(myJSON) print("Write successful")2
{ "domain": "tutswiki", "language": "python", "date": "11/09/2020", "topic": "config file" }