Appending
có nghĩa là tệp của bạn ngày càng dài hơn, đó không phải là những gì bạn cũng như cách JSON hoạt động.
Nếu bạn muốn cập nhật một số giá trị, bạn cần tải tệp JSON, hãy cập nhật [các] giá trị của bạn và bỏ lại:
with open['pre_database/playlist.json', 'r'] as f:
playlist = json.load[f]
playlist[key] = value # or whatever
with open['pre_database/playlist.json', 'w'] as f:
json.dump[playlist, f]
Ngoài ra, kiểm tra của bạn nếu tệp của bạn tồn tại sẽ xảy ra trước khi bạn mở tệp chứ không phải khi nó đã mở:
if os.path.exists['pre_database/playlist.json']:
with open['pre_database/playlist.json', 'r'] as f:
playlist = json.load[f]
playlist[key] = value # or whatever
with open['pre_database/playlist.json', 'w'] as f:
json.dump[playlist, f]
Mặc dù tôi đoán các cách Pythonic sẽ chỉ thử nó và bắt IOError
nếu tệp không tồn tại như mong đợi.
Tùy thuộc vào cách bạn tiếp tục, có lẽ tốt nhất là làm điều gì đó như thế này:
try:
with open['pre_database/playlist.json', 'r'] as f:
playlist = json.load[f]
except IOError, ValueError:
playlist = default_playlist
playlist[key] = value # or whatever
with open['pre_database/playlist.json', 'w'] as f:
json.dump[playlist, f]
Robin
Trong. Sự định nghĩa. Tab, chọn. Cập nhật. như hoạt động nhiệm vụ ..
Thực hiện theo quy trình tương tự là tạo tệp đích JSON bằng cách sử dụng thao tác Chèn tác vụ để cập nhật tệp JSON hiện có ..
Làm cách nào để thêm dữ liệu vào đối tượng JSON hiện có trong Python?
Trong. Sự định nghĩa. Tab, chọn. Cập nhật. như hoạt động nhiệm vụ ..
Thực hiện theo quy trình tương tự là tạo tệp đích JSON bằng cách sử dụng thao tác Chèn tác vụ để cập nhật tệp JSON hiện có ..
Làm cách nào để thêm dữ liệu vào đối tượng JSON hiện có trong Python?
Phương pháp 1: Sử dụng JSON ..
Nhập thư viện JSON với JSON nhập khẩu. ....
- Đọc tệp JSON trong cấu trúc dữ liệu bằng Data = JSON. ....json.loads[] function is present in python built-in ‘json’ module. This function is used to parse the JSON
string.
Cập nhật cấu trúc dữ liệu Python với mục nhập mới [ví dụ: từ điển mới để nối vào danh sách]. .... json.loads[json_string]
Parameter: It takes JSON string as the parameter.
Return type: It returns the python dictionary object.
- Viết dữ liệu JSON được cập nhật trở lại tệp JSON bằng JSON ..json.dumps[] function is present in python built-in ‘json’ module. This function is used to convert Python object into JSON string.
Chúng ta có thể chỉnh sửa JSON trong Python không? json.dumps[object]
Parameter: It takes Python Object as the parameter.
Return type: It returns the JSON string.
- 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]. 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
________ 7 ________ 8 & nbsp; '{
if os.path.exists['pre_database/playlist.json']:
with open['pre_database/playlist.json', 'r'] as f:
playlist = json.load[f]
playlist[key] = value # or whatever
with open['pre_database/playlist.json', 'w'] as f:
json.dump[playlist, f]
0____111121213if os.path.exists['pre_database/playlist.json']:
with open['pre_database/playlist.json', 'r'] as f:
playlist = json.load[f]
playlist[key] = value # or whatever
with open['pre_database/playlist.json', 'w'] as f:
json.dump[playlist, f]
4if os.path.exists['pre_database/playlist.json']:
with open['pre_database/playlist.json', 'r'] as f:
playlist = json.load[f]
playlist[key] = value # or whatever
with open['pre_database/playlist.json', 'w'] as f:
json.dump[playlist, f]
5if os.path.exists['pre_database/playlist.json']:
with open['pre_database/playlist.json', 'r'] as f:
playlist = json.load[f]
playlist[key] = value # or whatever
with open['pre_database/playlist.json', 'w'] as f:
json.dump[playlist, f]
1if os.path.exists['pre_database/playlist.json']:
with open['pre_database/playlist.json', 'r'] as f:
playlist = json.load[f]
playlist[key] = value # or whatever
with open['pre_database/playlist.json', 'w'] as f:
json.dump[playlist, f]
7if os.path.exists['pre_database/playlist.json']:
with open['pre_database/playlist.json', 'r'] as f:
playlist = json.load[f]
playlist[key] = value # or whatever
with open['pre_database/playlist.json', 'w'] as f:
json.dump[playlist, f]
3if os.path.exists['pre_database/playlist.json']:
with open['pre_database/playlist.json', 'r'] as f:
playlist = json.load[f]
playlist[key] = value # or whatever
with open['pre_database/playlist.json', 'w'] as f:
json.dump[playlist, f]
4try:
with open['pre_database/playlist.json', 'r'] as f:
playlist = json.load[f]
except IOError, ValueError:
playlist = default_playlist
playlist[key] = value # or whatever
with open['pre_database/playlist.json', 'w'] as f:
json.dump[playlist, f]
0if os.path.exists['pre_database/playlist.json']:
with open['pre_database/playlist.json', 'r'] as f:
playlist = json.load[f]
playlist[key] = value # or whatever
with open['pre_database/playlist.json', 'w'] as f:
json.dump[playlist, f]
1try:
with open['pre_database/playlist.json', 'r'] as f:
playlist = json.load[f]
except IOError, ValueError:
playlist = default_playlist
playlist[key] = value # or whatever
with open['pre_database/playlist.json', 'w'] as f:
json.dump[playlist, f]
2try:
with open['pre_database/playlist.json', 'r'] as f:
playlist = json.load[f]
except IOError, ValueError:
playlist = default_playlist
playlist[key] = value # or whatever
with open['pre_database/playlist.json', 'w'] as f:
json.dump[playlist, f]
3try:
with open['pre_database/playlist.json', 'r'] as f:
playlist = json.load[f]
except IOError, ValueError:
playlist = default_playlist
playlist[key] = value # or whatever
with open['pre_database/playlist.json', 'w'] as f:
json.dump[playlist, f]
4=
try:
with open['pre_database/playlist.json', 'r'] as f:
playlist = json.load[f]
except IOError, ValueError:
playlist = default_playlist
playlist[key] = value # or whatever
with open['pre_database/playlist.json', 'w'] as f:
json.dump[playlist, f]
6try:
with open['pre_database/playlist.json', 'r'] as f:
playlist = json.load[f]
except IOError, ValueError:
playlist = default_playlist
playlist[key] = value # or whatever
with open['pre_database/playlist.json', 'w'] as f:
json.dump[playlist, f]
7if os.path.exists['pre_database/playlist.json']:
with open['pre_database/playlist.json', 'r'] as f:
playlist = json.load[f]
playlist[key] = value # or whatever
with open['pre_database/playlist.json', 'w'] as f:
json.dump[playlist, f]
1try:
with open['pre_database/playlist.json', 'r'] as f:
playlist = json.load[f]
except IOError, ValueError:
playlist = default_playlist
playlist[key] = value # or whatever
with open['pre_database/playlist.json', 'w'] as f:
json.dump[playlist, f]
9Appending
0Appending
1=
Appending
3
Appending
4
Appending
5Appending
6
Output:
{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
Appending
9 IOError
0=
IOError
2IOError
3
IOError
4IOError
5IOError
6IOError
7IOError
8IOError
9import
0
if os.path.exists['pre_database/playlist.json']:
with open['pre_database/playlist.json', 'r'] as f:
playlist = json.load[f]
playlist[key] = value # or whatever
with open['pre_database/playlist.json', 'w'] as f:
json.dump[playlist, f]
1if os.path.exists['pre_database/playlist.json']:
with open['pre_database/playlist.json', 'r'] as f:
playlist = json.load[f]
playlist[key] = value # or whatever
with open['pre_database/playlist.json', 'w'] as f:
json.dump[playlist, f]
4import
3=
import
5import
0import
7if os.path.exists['pre_database/playlist.json']:
with open['pre_database/playlist.json', 'r'] as f:
playlist = json.load[f]
playlist[key] = value # or whatever
with open['pre_database/playlist.json', 'w'] as f:
json.dump[playlist, f]
4import
9json
0json
1if os.path.exists['pre_database/playlist.json']:
with open['pre_database/playlist.json', 'r'] as f:
playlist = json.load[f]
playlist[key] = value # or whatever
with open['pre_database/playlist.json', 'w'] as f:
json.dump[playlist, f]
4import
0json
4json
5import
7if os.path.exists['pre_database/playlist.json']:
with open['pre_database/playlist.json', 'r'] as f:
playlist = json.load[f]
playlist[key] = value # or whatever
with open['pre_database/playlist.json', 'w'] as f:
json.dump[playlist, f]
4json
8import
0x
0=
x
2import
7try:
with open['pre_database/playlist.json', 'r'] as f:
playlist = json.load[f]
except IOError, ValueError:
playlist = default_playlist
playlist[key] = value # or whatever
with open['pre_database/playlist.json', 'w'] as f:
json.dump[playlist, f]
4=
try:
with open['pre_database/playlist.json', 'r'] as f:
playlist = json.load[f]
except IOError, ValueError:
playlist = default_playlist
playlist[key] = value # or whatever
with open['pre_database/playlist.json', 'w'] as f:
json.dump[playlist, f]
6x
7___=
1=
2=
3=
4
if os.path.exists['pre_database/playlist.json']:
with open['pre_database/playlist.json', 'r'] as f:
playlist = json.load[f]
playlist[key] = value # or whatever
with open['pre_database/playlist.json', 'w'] as f:
json.dump[playlist, f]
3=
1=
7=
3=
9
IOError
4Appending
0
'{
2
Output: