Hướng dẫn update file json python - cập nhật tệp json python

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)
4 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.

Show

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:

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)
41
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)
9
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)
43

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. 

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.

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)
49
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)
50
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)
9
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)
52
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)
53

  • 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)
    
    54
    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)
    
    55
    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)
    
    56
    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)
    
    57
    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)
    
    58
    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)
    
    59
    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)
    
    60
  • 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)
    
    1
    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)
    
    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)
    
    63
    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)
    
    9
    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)
    
    65
    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)
    
    60
    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)
    
    67
    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)
    
    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)
    
    69
    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)
    
    70
    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)
    
    71
    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)
    
    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)
    
    60
    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)
    
    74
    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)
    
    75
    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)
    
    67
    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)
    
    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)
    
    78
    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)
    
    60
    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)
    
    50
    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)
    
    9
    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)
    
    52
    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)
    
    67
    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)
    
    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)
    
    9
    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)
    
    6
    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)
    
    57___
  • 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.

    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)
    
    49
    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)
    
    50
    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)
    
    9
    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)
    
    52
    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)
    
    53

    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)
    
    54
    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)
    
    55
    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)
    
    56
    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)
    
    57
    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)
    
    58
    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)
    
    59
    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)
    
    60
     

    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) 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) 63if 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) 9 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) 65if 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) 60if 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) 67if 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) 69if 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) 70if 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) 71if 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) 60if 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) 74if 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) 75if 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) 67if 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) 78if 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) 60if 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) 50if 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) 9 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) 52if 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) 67try: 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) 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) 9try: 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) 6if 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) 57___

    • 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).edit JSON file using open(), append(), dump() and close() functions. so let's see a simple example below: You can use these examples with python3 (Python 3) version.json.loads() function is present in python built-in ‘json’ module. This function is used to parse the JSON string.
       

    Trong. Sự định nghĩa. Tab, chọn. Cập nhật. như hoạt động nhiệm vụ .. 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. 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.   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.  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.   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.   Updating a JSON string.
      

    Python3

    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)
    
    6
    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)
    
    7

    ________ 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)
    
    8

    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____111121213
    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)
    
    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)
    
    5
    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)
    
    1
    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)
    
    7
    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
    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)
    
    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)
    
    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)
    
    1
    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)
    
    2
    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)
    
    3
    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)
    
    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)
    
    9
    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)
    
    6
    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)
    
    7
    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)
    
    1
    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)
    
    9
    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)
    
    40

    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)
    
    41
    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)
    
    9
    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)
    
    43

    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)
    
    44

    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)
    
    45
    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)
    
    46

    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.  Updating a JSON file. Suppose the JSON file looks like this.
     

    Hướng dẫn update file json python - cập nhật tệp json python

    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

    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)
    
    6
    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)
    
    7

    ________ 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)
    
    8

    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____111121213
    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)
    
    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)
    
    5
    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)
    
    1
    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)
    
    7
    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
    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)
    
    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)
    
    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)
    
    1
    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)
    
    2
    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)
    
    3
    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)
    
    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)
    
    9
    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)
    
    6
    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)
    
    7
    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)
    
    1
    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)
    
    9
    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)
    
    40

    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)
    
    41
    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)
    
    9
    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)
    
    43

    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)
    
    91
    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)
    
    92
    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)
    
    93
    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)
    
    94

    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

    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)
    
    91
    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)
    
    97
    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)
    
    93
    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)
    
    99

    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)
    
    54
    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)
    
    40

    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)
    
    82

    Output:  
     


    {Pin Pin Cảnh: 110096, Tổ chức của người Hồi giáo edit JSON file using open(), append(), dump() and close() functions. so let's see a simple example below: You can use these examples with python3 (Python 3) version.