Tôi đang cố cập nhật tệp JSON hiện có, nhưng từ một số lý do, giá trị được yêu cầu không được thay đổi nhưng toàn bộ tập hợp các giá trị [với giá trị mới] đang được thêm vào tệp gốc
jsonFile = open["replayScript.json", "r+"]
data = json.load[jsonFile]
tmp = data["location"]
data["location"] = "NewPath"
jsonFile.write[json.dumps[data]]
và kết quả là: Yêu cầu:
{
"location": "NewPath",
"Id": "0",
"resultDir": "",
"resultFile": "",
"mode": "replay",
"className": "",
"method": "METHOD"
}
Actual:
{
"location": "/home/karim/storm/project/storm/devqa/default.xml",
"Id": "0",
"resultDir": "",
"resultFile": "",
"mode": "replay",
"className": "",
"method": "METHOD"
}
{
"resultDir": "",
"location": "pathaaaaaaaaaaaaaaaaaaaaaaaaa",
"method": "METHOD",
"className": "",
"mode": "replay",
"Id": "0",
"resultFile": ""
}
Cập nhật nội dung tệp JSON của Python với các ví dụ mã Xin chào mọi người, trong bài đăng này, chúng tôi sẽ kiểm tra cách giải câu đố lập trình nội dung tệp JSON cập nhật Python. Chúng tôi đã trình bày rất nhiều ví dụ minh họa để cho thấy cách giải quyết vấn đề nội dung tệp JSON của Python Cập nhật và chúng tôi cũng đã giải thích cách làm như vậy.import json
def repJsonObj[file, object, value]:
f = open[f"{file}", "r"]
json_object = json.load[f]
f.close[]
json_object[object] = value
f = open[file, "w"]
json.dump[json_object, f]
f.close[]
repJsonObj['test.json', "num", 2713490127]
'{
0'{
6'{
22
Bạn có thể chỉnh sửa tệp JSON bằng Python không?
'{
0'{
6'{
22
Bạn có thể chỉnh sửa tệp JSON bằng Python không?
Có một số cách để cập nhật tệp JSON trong Python. Ở đây, tôi sẽ cung cấp cho bạn ví dụ rất đơn giản về tệp JSON chỉnh sửa bằng các hàm Open [], append [], dump [] và đóng []. Vì vậy, hãy xem một ví dụ đơn giản dưới đây: bạn có thể sử dụng các ví dụ này với phiên bản Python3 [Python 3].
Các chức năng được sử dụng: & nbsp; & nbsp;
- json.loads []: hàm json.loads [] có mặt trong mô-đun python tích hợp ‘json. Hàm này được sử dụng để phân tích chuỗi JSON. & NBSP;json.loads[] function is present in python built-in ‘json’ module. This function is used to parse the JSON string.
Cú pháp: tham số JSON.LOADS [JSON_STRING]: Nó lấy chuỗi JSON làm tham số.return loại: nó trả về đối tượng từ điển Python. & Nbsp; & nbsp; json.loads[json_string]
Parameter: It takes JSON string as the parameter.
Return type: It returns the python dictionary object.
- json.dumps []: hàm json.dumps [] có mặt trong mô-đun python tích hợp ‘json. Hàm này được sử dụng để chuyển đổi đối tượng Python thành Chuỗi JSON. & NBSP;json.dumps[] function is present in python built-in ‘json’ module. This function is used to convert Python object into JSON string.
Cú pháp: json.dumps [Đối tượng] Tham số: Nó lấy đối tượng Python làm tham số.return loại: nó trả về chuỗi JSON. & Nbsp; & nbsp; json.dumps[object]
Parameter: It takes Python Object as the parameter.
Return type: It returns the JSON string.
- CẬP NHẬT []: Phương thức này cập nhật từ điển với các phần tử từ một đối tượng từ điển khác hoặc từ một cặp khóa/giá trị có thể lặp lại. & NBSP; This method updates the dictionary with elements from another dictionary object or from an iterable key/value pair.
Cú pháp: Dict.Update [[Khác]] Tham số: Lấy một từ điển khác hoặc một cặp khóa/giá trị có thể lặp lại. dict.update[[other]]
Parameters: Takes another dictionary or an iterable key/value pair.
Return type: Returns None.
Ví dụ 1: Cập nhật chuỗi JSON. & NBSP; & NBSP; Updating a JSON string.
Python3
import
json
________ 6 ________ 7 & nbsp; '{
"organization"
____1011
{
"location": "NewPath",
"Id": "0",
"resultDir": "",
"resultFile": "",
"mode": "replay",
"className": "",
"method": "METHOD"
}
3{
"location": "NewPath",
"Id": "0",
"resultDir": "",
"resultFile": "",
"mode": "replay",
"className": "",
"method": "METHOD"
}
4{
"location": "NewPath",
"Id": "0",
"resultDir": "",
"resultFile": "",
"mode": "replay",
"className": "",
"method": "METHOD"
}
0{
"location": "NewPath",
"Id": "0",
"resultDir": "",
"resultFile": "",
"mode": "replay",
"className": "",
"method": "METHOD"
}
6{
"location": "NewPath",
"Id": "0",
"resultDir": "",
"resultFile": "",
"mode": "replay",
"className": "",
"method": "METHOD"
}
2{
"location": "NewPath",
"Id": "0",
"resultDir": "",
"resultFile": "",
"mode": "replay",
"className": "",
"method": "METHOD"
}
3{
"location": "NewPath",
"Id": "0",
"resultDir": "",
"resultFile": "",
"mode": "replay",
"className": "",
"method": "METHOD"
}
9{
"location": "NewPath",
"Id": "0",
"resultDir": "",
"resultFile": "",
"mode": "replay",
"className": "",
"method": "METHOD"
}
0{
"location": "/home/karim/storm/project/storm/devqa/default.xml",
"Id": "0",
"resultDir": "",
"resultFile": "",
"mode": "replay",
"className": "",
"method": "METHOD"
}
{
"resultDir": "",
"location": "pathaaaaaaaaaaaaaaaaaaaaaaaaa",
"method": "METHOD",
"className": "",
"mode": "replay",
"Id": "0",
"resultFile": ""
}
1{
"location": "/home/karim/storm/project/storm/devqa/default.xml",
"Id": "0",
"resultDir": "",
"resultFile": "",
"mode": "replay",
"className": "",
"method": "METHOD"
}
{
"resultDir": "",
"location": "pathaaaaaaaaaaaaaaaaaaaaaaaaa",
"method": "METHOD",
"className": "",
"mode": "replay",
"Id": "0",
"resultFile": ""
}
2{
"location": "/home/karim/storm/project/storm/devqa/default.xml",
"Id": "0",
"resultDir": "",
"resultFile": "",
"mode": "replay",
"className": "",
"method": "METHOD"
}
{
"resultDir": "",
"location": "pathaaaaaaaaaaaaaaaaaaaaaaaaa",
"method": "METHOD",
"className": "",
"mode": "replay",
"Id": "0",
"resultFile": ""
}
3=
{
"location": "/home/karim/storm/project/storm/devqa/default.xml",
"Id": "0",
"resultDir": "",
"resultFile": "",
"mode": "replay",
"className": "",
"method": "METHOD"
}
{
"resultDir": "",
"location": "pathaaaaaaaaaaaaaaaaaaaaaaaaa",
"method": "METHOD",
"className": "",
"mode": "replay",
"Id": "0",
"resultFile": ""
}
5{
"location": "/home/karim/storm/project/storm/devqa/default.xml",
"Id": "0",
"resultDir": "",
"resultFile": "",
"mode": "replay",
"className": "",
"method": "METHOD"
}
{
"resultDir": "",
"location": "pathaaaaaaaaaaaaaaaaaaaaaaaaa",
"method": "METHOD",
"className": "",
"mode": "replay",
"Id": "0",
"resultFile": ""
}
6{
"location": "NewPath",
"Id": "0",
"resultDir": "",
"resultFile": "",
"mode": "replay",
"className": "",
"method": "METHOD"
}
0{
"location": "/home/karim/storm/project/storm/devqa/default.xml",
"Id": "0",
"resultDir": "",
"resultFile": "",
"mode": "replay",
"className": "",
"method": "METHOD"
}
{
"resultDir": "",
"location": "pathaaaaaaaaaaaaaaaaaaaaaaaaa",
"method": "METHOD",
"className": "",
"mode": "replay",
"Id": "0",
"resultFile": ""
}
8{
"location": "/home/karim/storm/project/storm/devqa/default.xml",
"Id": "0",
"resultDir": "",
"resultFile": "",
"mode": "replay",
"className": "",
"method": "METHOD"
}
{
"resultDir": "",
"location": "pathaaaaaaaaaaaaaaaaaaaaaaaaa",
"method": "METHOD",
"className": "",
"mode": "replay",
"Id": "0",
"resultFile": ""
}
9import json def repJsonObj[file, object, value]: f = open[f"{file}", "r"] json_object = json.load[f] f.close[] json_object[object] = value f = open[file, "w"] json.dump[json_object, f] f.close[] repJsonObj['test.json', "num", 2713490127]0____7
import json def repJsonObj[file, object, value]: f = open[f"{file}", "r"] json_object = json.load[f] f.close[] json_object[object] = value f = open[file, "w"] json.dump[json_object, f] f.close[] repJsonObj['test.json', "num", 2713490127]2
import json def repJsonObj[file, object, value]: f = open[f"{file}", "r"] json_object = json.load[f] f.close[] json_object[object] = value f = open[file, "w"] json.dump[json_object, f] f.close[] repJsonObj['test.json', "num", 2713490127]3
import json def repJsonObj[file, object, value]: f = open[f"{file}", "r"] json_object = json.load[f] f.close[] json_object[object] = value f = open[file, "w"] json.dump[json_object, f] f.close[] repJsonObj['test.json', "num", 2713490127]4
import json def repJsonObj[file, object, value]: f = open[f"{file}", "r"] json_object = json.load[f] f.close[] json_object[object] = value f = open[file, "w"] json.dump[json_object, f] f.close[] repJsonObj['test.json', "num", 2713490127]5
Đầu ra: & nbsp;
{Pin Pin Cảnh: 110096, Tổ chức của người Hồi giáo
Ví dụ 2: Cập nhật tệp JSON. Giả sử tệp JSON trông như thế này. & NBSP; Updating a JSON file. Suppose the JSON file looks like this.
Chúng tôi muốn thêm một dữ liệu JSON khác sau EMP_DETAILS. Dưới đây là việc thực hiện.
Python3
import
json
import json def repJsonObj[file, object, value]: f = open[f"{file}", "r"] json_object = json.load[f] f.close[] json_object[object] = value f = open[file, "w"] json.dump[json_object, f] f.close[] repJsonObj['test.json', "num", 2713490127]8
import json def repJsonObj[file, object, value]: f = open[f"{file}", "r"] json_object = json.load[f] f.close[] json_object[object] = value f = open[file, "w"] json.dump[json_object, f] f.close[] repJsonObj['test.json', "num", 2713490127]9
=
import
1import
2import
3import
4import
5import
6import
7import
8import
9
{
"location": "NewPath",
"Id": "0",
"resultDir": "",
"resultFile": "",
"mode": "replay",
"className": "",
"method": "METHOD"
}
0{
"location": "NewPath",
"Id": "0",
"resultDir": "",
"resultFile": "",
"mode": "replay",
"className": "",
"method": "METHOD"
}
3json
22 json
4import
9json
6{
"location": "NewPath",
"Id": "0",
"resultDir": "",
"resultFile": "",
"mode": "replay",
"className": "",
"method": "METHOD"
}
3json
8json
9x
0{
"location": "NewPath",
"Id": "0",
"resultDir": "",
"resultFile": "",
"mode": "replay",
"className": "",
"method": "METHOD"
}
3import
9x
3x
4json
6{
"location": "NewPath",
"Id": "0",
"resultDir": "",
"resultFile": "",
"mode": "replay",
"className": "",
"method": "METHOD"
}
3x
7import
9x
9=
=
1json
6Các
'{
0'{
1'{
2'{
3
{
"location": "NewPath",
"Id": "0",
"resultDir": "",
"resultFile": "",
"mode": "replay",
"className": "",
"method": "METHOD"
}
2'{
0'{
6'{
22
import
3
{
"location": "/home/karim/storm/project/storm/devqa/default.xml",
"Id": "0",
"resultDir": "",
"resultFile": "",
"mode": "replay",
"className": "",
"method": "METHOD"
}
{
"resultDir": "",
"location": "pathaaaaaaaaaaaaaaaaaaaaaaaaa",
"method": "METHOD",
"className": "",
"mode": "replay",
"Id": "0",
"resultFile": ""
}
9"organization"
1
Output: