Hướng dẫn save dictionary as json python - lưu từ điển dưới dạng json python

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc
    Note: For more information, refer to Read, Write and Parse JSON using Python
     

    Bàn luận  

    • json.dumps[]  
       
    • json.dump[]  
       

    JSON là viết tắt của ký hiệu đối tượng JavaScript. Điều đó có nghĩa là một tệp tập lệnh [thực thi] được tạo bằng văn bản bằng ngôn ngữ lập trình, được sử dụng để lưu trữ và chuyển dữ liệu. Python hỗ trợ JSON thông qua gói tích hợp có tên JSON. Để sử dụng tính năng này, chúng tôi nhập gói JSON trong tập lệnh Python. Văn bản trong JSON được thực hiện thông qua chuỗi được trích dẫn có chứa giá trị trong ánh xạ giá trị khóa trong {}. Nó tương tự như từ điển trong Python.note: Để biết thêm thông tin, hãy tham khảo đọc, viết và phân tích JSON bằng Python & NBSP;json.dumps[dict, indent]
    Parameters: 
     

    • Chức năng được sử dụng: & nbsp; & nbsp; name of dictionary which should be converted to JSON object. 
       
    • Cú pháp: json.dumps [dict, thụt lề] tham số: & nbsp; & nbsp; defines the number of units for indentation 
       

    Từ điển - Tên của Từ điển nên được chuyển đổi thành đối tượng JSON. & nbsp; & nbsp; json.dump[dict, file_pointer]
    Parameters: 
     

    • Chức năng được sử dụng: & nbsp; & nbsp; name of dictionary which should be converted to JSON object. 
       
    • Cú pháp: json.dumps [dict, thụt lề] tham số: & nbsp; & nbsp; pointer of the file opened in write or append mode. 
       

    Từ điển - Tên của Từ điển nên được chuyển đổi thành đối tượng JSON. & nbsp; & nbsp;

    Python3

    thụt lề - Xác định số lượng đơn vị cho thụt lề & nbsp; & nbsp;

    dictionary =

    {
        "department": "HR",
        "id": "04",
        "name": "sunil"
    }
    0

    Cú pháp: tham số json.dump [dict, file_pulum]: & nbsp; & nbsp;

    {
        "department": "HR",
        "id": "04",
        "name": "sunil"
    }
    1
    {
        "department": "HR",
        "id": "04",
        "name": "sunil"
    }
    7
    {
        "department": "HR",
        "id": "04",
        "name": "sunil"
    }
    3
    {
        "department": "HR",
        "id": "04",
        "name": "sunil"
    }
    9
    {
        "department": "HR",
        "id": "04",
        "name": "sunil"
    }
    5

    {
        "department": "HR",
        "id": "04",
        "name": "sunil"
    }
    1
    import ConfigParser
    
    config = ConfigParser.ConfigParser[]
    
    dict1 = {'key1':'keyinfo', 'key2':'keyinfo2'}
    dict2 = {'k1':'hot', 'k2':'cross', 'k3':'buns'}
    dict3 = {'x':1, 'y':2, 'z':3}
    
    # Make each dictionary a separate section in the configuration
    config.add_section['dict1']
    for key in dict1.keys[]:
        config.set['dict1', key, dict1[key]]
       
    config.add_section['dict2']
    for key in dict2.keys[]:
        config.set['dict2', key, dict2[key]]
    
    config.add_section['dict3']
    for key in dict3.keys[]:
        config.set['dict3', key, dict3[key]]
    
    # Save the configuration to a file
    f = open['config.ini', 'w']
    config.write[f]
    f.close[]
    
    # Read the configuration from a file
    config2 = ConfigParser.ConfigParser[]
    config2.read['config.ini']
    
    dictA = {}
    for item in config2.items['dict1']:
        dictA[item[0]] = item[1]
    
    dictB = {}
    for item in config2.items['dict2']:
        dictB[item[0]] = item[1]
    
    dictC = {}
    for item in config2.items['dict3']:
        dictC[item[0]] = item[1]
    
    print[dictA]
    print[dictB]
    print[dictC]
    
    2
    {
        "department": "HR",
        "id": "04",
        "name": "sunil"
    }
    3
    import ConfigParser
    
    config = ConfigParser.ConfigParser[]
    
    dict1 = {'key1':'keyinfo', 'key2':'keyinfo2'}
    dict2 = {'k1':'hot', 'k2':'cross', 'k3':'buns'}
    dict3 = {'x':1, 'y':2, 'z':3}
    
    # Make each dictionary a separate section in the configuration
    config.add_section['dict1']
    for key in dict1.keys[]:
        config.set['dict1', key, dict1[key]]
       
    config.add_section['dict2']
    for key in dict2.keys[]:
        config.set['dict2', key, dict2[key]]
    
    config.add_section['dict3']
    for key in dict3.keys[]:
        config.set['dict3', key, dict3[key]]
    
    # Save the configuration to a file
    f = open['config.ini', 'w']
    config.write[f]
    f.close[]
    
    # Read the configuration from a file
    config2 = ConfigParser.ConfigParser[]
    config2.read['config.ini']
    
    dictA = {}
    for item in config2.items['dict1']:
        dictA[item[0]] = item[1]
    
    dictB = {}
    for item in config2.items['dict2']:
        dictB[item[0]] = item[1]
    
    dictC = {}
    for item in config2.items['dict3']:
        dictC[item[0]] = item[1]
    
    print[dictA]
    print[dictB]
    print[dictC]
    
    4

    import ConfigParser
    
    config = ConfigParser.ConfigParser[]
    
    dict1 = {'key1':'keyinfo', 'key2':'keyinfo2'}
    dict2 = {'k1':'hot', 'k2':'cross', 'k3':'buns'}
    dict3 = {'x':1, 'y':2, 'z':3}
    
    # Make each dictionary a separate section in the configuration
    config.add_section['dict1']
    for key in dict1.keys[]:
        config.set['dict1', key, dict1[key]]
       
    config.add_section['dict2']
    for key in dict2.keys[]:
        config.set['dict2', key, dict2[key]]
    
    config.add_section['dict3']
    for key in dict3.keys[]:
        config.set['dict3', key, dict3[key]]
    
    # Save the configuration to a file
    f = open['config.ini', 'w']
    config.write[f]
    f.close[]
    
    # Read the configuration from a file
    config2 = ConfigParser.ConfigParser[]
    config2.read['config.ini']
    
    dictA = {}
    for item in config2.items['dict1']:
        dictA[item[0]] = item[1]
    
    dictB = {}
    for item in config2.items['dict2']:
        dictB[item[0]] = item[1]
    
    dictC = {}
    for item in config2.items['dict3']:
        dictC[item[0]] = item[1]
    
    print[dictA]
    print[dictB]
    print[dictC]
    
    5

    Con trỏ tệp - Con trỏ của tệp được mở ở chế độ ghi hoặc chắp thêm. & nbsp; & nbsp;

    import configparser
    
    config = configparser.ConfigParser[]
    
    dict1 = {'key1':'keyinfo', 'key2':'keyinfo2'}
    dict2 = {'k1':'hot', 'k2':'cross', 'k3':'buns'}
    dict3 = {'x':1, 'y':2, 'z':3}
    
    # Make each dictionary a separate section in the configuration
    config['dict1'] = dict1
    config['dict2'] = dict2
    config['dict3'] = dict3
    
    # Save the configuration to a file
    f = open['config.ini', 'w']
    config.write[f]
    f.close[]
    
    # Read the configuration from a file
    config2 = configparser.ConfigParser[]
    config2.read['config.ini']
    
    # ConfigParser objects are a lot like dictionaries, but if you really
    # want a dictionary you can ask it to convert a section to a dictionary
    dictA = dict[config2['dict1'] ]
    dictB = dict[config2['dict2'] ]
    dictC = dict[config2['dict3']]
    
    print[dictA]
    print[dictB]
    print[dictC]
    
    2
    import configparser
    
    config = configparser.ConfigParser[]
    
    dict1 = {'key1':'keyinfo', 'key2':'keyinfo2'}
    dict2 = {'k1':'hot', 'k2':'cross', 'k3':'buns'}
    dict3 = {'x':1, 'y':2, 'z':3}
    
    # Make each dictionary a separate section in the configuration
    config['dict1'] = dict1
    config['dict2'] = dict2
    config['dict3'] = dict3
    
    # Save the configuration to a file
    f = open['config.ini', 'w']
    config.write[f]
    f.close[]
    
    # Read the configuration from a file
    config2 = configparser.ConfigParser[]
    config2.read['config.ini']
    
    # ConfigParser objects are a lot like dictionaries, but if you really
    # want a dictionary you can ask it to convert a section to a dictionary
    dictA = dict[config2['dict1'] ]
    dictB = dict[config2['dict2'] ]
    dictC = dict[config2['dict3']]
    
    print[dictA]
    print[dictB]
    print[dictC]
    
    3

    Ví dụ 1:

    {
        "id": "04",
        "name": "sunil",
        "department": "HR"
    }

    Output:

    {
        "department": "HR",
        "id": "04",
        "name": "sunil"
    }

    import json 

    Python3

    {
        "department": "HR",
        "id": "04",
        "name": "sunil"
    }
    1
    {
        "department": "HR",
        "id": "04",
        "name": "sunil"
    }
    2
    {
        "department": "HR",
        "id": "04",
        "name": "sunil"
    }
    3
    {
        "department": "HR",
        "id": "04",
        "name": "sunil"
    }
    4
    {
        "department": "HR",
        "id": "04",
        "name": "sunil"
    }
    5

    dictionary =

    import configparser
    
    config = configparser.ConfigParser[]
    
    dict1 = {'key1':'keyinfo', 'key2':'keyinfo2'}
    dict2 = {'k1':'hot', 'k2':'cross', 'k3':'buns'}
    dict3 = {'x':1, 'y':2, 'z':3}
    
    # Make each dictionary a separate section in the configuration
    config['dict1'] = dict1
    config['dict2'] = dict2
    config['dict3'] = dict3
    
    # Save the configuration to a file
    f = open['config.ini', 'w']
    config.write[f]
    f.close[]
    
    # Read the configuration from a file
    config2 = configparser.ConfigParser[]
    config2.read['config.ini']
    
    # ConfigParser objects are a lot like dictionaries, but if you really
    # want a dictionary you can ask it to convert a section to a dictionary
    dictA = dict[config2['dict1'] ]
    dictB = dict[config2['dict2'] ]
    dictC = dict[config2['dict3']]
    
    print[dictA]
    print[dictB]
    print[dictC]
    
    8

    import ConfigParser
    
    config = ConfigParser.ConfigParser[]
    
    dict1 = {'key1':'keyinfo', 'key2':'keyinfo2'}
    dict2 = {'k1':'hot', 'k2':'cross', 'k3':'buns'}
    dict3 = {'x':1, 'y':2, 'z':3}
    
    # Make each dictionary a separate section in the configuration
    config.add_section['dict1']
    for key in dict1.keys[]:
        config.set['dict1', key, dict1[key]]
       
    config.add_section['dict2']
    for key in dict2.keys[]:
        config.set['dict2', key, dict2[key]]
    
    config.add_section['dict3']
    for key in dict3.keys[]:
        config.set['dict3', key, dict3[key]]
    
    # Save the configuration to a file
    f = open['config.ini', 'w']
    config.write[f]
    f.close[]
    
    # Read the configuration from a file
    config2 = ConfigParser.ConfigParser[]
    config2.read['config.ini']
    
    dictA = {}
    for item in config2.items['dict1']:
        dictA[item[0]] = item[1]
    
    dictB = {}
    for item in config2.items['dict2']:
        dictB[item[0]] = item[1]
    
    dictC = {}
    for item in config2.items['dict3']:
        dictC[item[0]] = item[1]
    
    print[dictA]
    print[dictB]
    print[dictC]
    
    6=
    import ConfigParser
    
    config = ConfigParser.ConfigParser[]
    
    dict1 = {'key1':'keyinfo', 'key2':'keyinfo2'}
    dict2 = {'k1':'hot', 'k2':'cross', 'k3':'buns'}
    dict3 = {'x':1, 'y':2, 'z':3}
    
    # Make each dictionary a separate section in the configuration
    config.add_section['dict1']
    for key in dict1.keys[]:
        config.set['dict1', key, dict1[key]]
       
    config.add_section['dict2']
    for key in dict2.keys[]:
        config.set['dict2', key, dict2[key]]
    
    config.add_section['dict3']
    for key in dict3.keys[]:
        config.set['dict3', key, dict3[key]]
    
    # Save the configuration to a file
    f = open['config.ini', 'w']
    config.write[f]
    f.close[]
    
    # Read the configuration from a file
    config2 = ConfigParser.ConfigParser[]
    config2.read['config.ini']
    
    dictA = {}
    for item in config2.items['dict1']:
        dictA[item[0]] = item[1]
    
    dictB = {}
    for item in config2.items['dict2']:
        dictB[item[0]] = item[1]
    
    dictC = {}
    for item in config2.items['dict3']:
        dictC[item[0]] = item[1]
    
    print[dictA]
    print[dictB]
    print[dictC]
    
    8=
    import configparser
    
    config = configparser.ConfigParser[]
    
    dict1 = {'key1':'keyinfo', 'key2':'keyinfo2'}
    dict2 = {'k1':'hot', 'k2':'cross', 'k3':'buns'}
    dict3 = {'x':1, 'y':2, 'z':3}
    
    # Make each dictionary a separate section in the configuration
    config['dict1'] = dict1
    config['dict2'] = dict2
    config['dict3'] = dict3
    
    # Save the configuration to a file
    f = open['config.ini', 'w']
    config.write[f]
    f.close[]
    
    # Read the configuration from a file
    config2 = configparser.ConfigParser[]
    config2.read['config.ini']
    
    # ConfigParser objects are a lot like dictionaries, but if you really
    # want a dictionary you can ask it to convert a section to a dictionary
    dictA = dict[config2['dict1'] ]
    dictB = dict[config2['dict2'] ]
    dictC = dict[config2['dict3']]
    
    print[dictA]
    print[dictB]
    print[dictC]
    
    0
    import configparser
    
    config = configparser.ConfigParser[]
    
    dict1 = {'key1':'keyinfo', 'key2':'keyinfo2'}
    dict2 = {'k1':'hot', 'k2':'cross', 'k3':'buns'}
    dict3 = {'x':1, 'y':2, 'z':3}
    
    # Make each dictionary a separate section in the configuration
    config['dict1'] = dict1
    config['dict2'] = dict2
    config['dict3'] = dict3
    
    # Save the configuration to a file
    f = open['config.ini', 'w']
    config.write[f]
    f.close[]
    
    # Read the configuration from a file
    config2 = configparser.ConfigParser[]
    config2.read['config.ini']
    
    # ConfigParser objects are a lot like dictionaries, but if you really
    # want a dictionary you can ask it to convert a section to a dictionary
    dictA = dict[config2['dict1'] ]
    dictB = dict[config2['dict2'] ]
    dictC = dict[config2['dict3']]
    
    print[dictA]
    print[dictB]
    print[dictC]
    
    1

    Đầu ra

    Ví dụ 2:

    import

    import configparser
    
    config = configparser.ConfigParser[]
    
    dict1 = {'key1':'keyinfo', 'key2':'keyinfo2'}
    dict2 = {'k1':'hot', 'k2':'cross', 'k3':'buns'}
    dict3 = {'x':1, 'y':2, 'z':3}
    
    # Make each dictionary a separate section in the configuration
    config['dict1'] = dict1
    config['dict2'] = dict2
    config['dict3'] = dict3
    
    # Save the configuration to a file
    f = open['config.ini', 'w']
    config.write[f]
    f.close[]
    
    # Read the configuration from a file
    config2 = configparser.ConfigParser[]
    config2.read['config.ini']
    
    # ConfigParser objects are a lot like dictionaries, but if you really
    # want a dictionary you can ask it to convert a section to a dictionary
    dictA = dict[config2['dict1'] ]
    dictB = dict[config2['dict2'] ]
    dictC = dict[config2['dict3']]
    
    print[dictA]
    print[dictB]
    print[dictC]
    
    5

    [dict1]
    key2 = keyinfo2
    key1 = keyinfo
    
    [dict2]
    k1 = hot
    k2 = cross
    k3 = buns
    
    [dict3]
    z = 3
    y = 2
    x = 1
    
    8

    [dict1]
    key2 = keyinfo2
    key1 = keyinfo
    
    [dict2]
    k1 = hot
    k2 = cross
    k3 = buns
    
    [dict3]
    z = 3
    y = 2
    x = 1
    
    9import0import1import2import3import4import5

    import configparser
    
    config = configparser.ConfigParser[]
    
    dict1 = {'key1':'keyinfo', 'key2':'keyinfo2'}
    dict2 = {'k1':'hot', 'k2':'cross', 'k3':'buns'}
    dict3 = {'x':1, 'y':2, 'z':3}
    
    # Make each dictionary a separate section in the configuration
    config['dict1'] = dict1
    config['dict2'] = dict2
    config['dict3'] = dict3
    
    # Save the configuration to a file
    f = open['config.ini', 'w']
    config.write[f]
    f.close[]
    
    # Read the configuration from a file
    config2 = configparser.ConfigParser[]
    config2.read['config.ini']
    
    # ConfigParser objects are a lot like dictionaries, but if you really
    # want a dictionary you can ask it to convert a section to a dictionary
    dictA = dict[config2['dict1'] ]
    dictB = dict[config2['dict2'] ]
    dictC = dict[config2['dict3']]
    
    print[dictA]
    print[dictB]
    print[dictC]
    
    9import7

    Output: 
     


    Để hoàn thiện, chúng ta nên bao gồm ConfigParser và ConfigParser, một phần của thư viện tiêu chuẩn trong Python 2 và 3, tương ứng. Mô -đun này đọc và ghi vào tệp config/ini và [ít nhất là trong Python 3] hoạt động theo nhiều cách như từ điển. Nó có thêm lợi ích mà bạn có thể lưu trữ nhiều từ điển vào các phần riêng biệt của tệp config/ini của bạn và nhớ lại chúng. Ngọt!

    Ví dụ Python 2.7.x.

    import ConfigParser
    
    config = ConfigParser.ConfigParser[]
    
    dict1 = {'key1':'keyinfo', 'key2':'keyinfo2'}
    dict2 = {'k1':'hot', 'k2':'cross', 'k3':'buns'}
    dict3 = {'x':1, 'y':2, 'z':3}
    
    # Make each dictionary a separate section in the configuration
    config.add_section['dict1']
    for key in dict1.keys[]:
        config.set['dict1', key, dict1[key]]
       
    config.add_section['dict2']
    for key in dict2.keys[]:
        config.set['dict2', key, dict2[key]]
    
    config.add_section['dict3']
    for key in dict3.keys[]:
        config.set['dict3', key, dict3[key]]
    
    # Save the configuration to a file
    f = open['config.ini', 'w']
    config.write[f]
    f.close[]
    
    # Read the configuration from a file
    config2 = ConfigParser.ConfigParser[]
    config2.read['config.ini']
    
    dictA = {}
    for item in config2.items['dict1']:
        dictA[item[0]] = item[1]
    
    dictB = {}
    for item in config2.items['dict2']:
        dictB[item[0]] = item[1]
    
    dictC = {}
    for item in config2.items['dict3']:
        dictC[item[0]] = item[1]
    
    print[dictA]
    print[dictB]
    print[dictC]
    

    Ví dụ Python 3.x.

    import configparser
    
    config = configparser.ConfigParser[]
    
    dict1 = {'key1':'keyinfo', 'key2':'keyinfo2'}
    dict2 = {'k1':'hot', 'k2':'cross', 'k3':'buns'}
    dict3 = {'x':1, 'y':2, 'z':3}
    
    # Make each dictionary a separate section in the configuration
    config['dict1'] = dict1
    config['dict2'] = dict2
    config['dict3'] = dict3
    
    # Save the configuration to a file
    f = open['config.ini', 'w']
    config.write[f]
    f.close[]
    
    # Read the configuration from a file
    config2 = configparser.ConfigParser[]
    config2.read['config.ini']
    
    # ConfigParser objects are a lot like dictionaries, but if you really
    # want a dictionary you can ask it to convert a section to a dictionary
    dictA = dict[config2['dict1'] ]
    dictB = dict[config2['dict2'] ]
    dictC = dict[config2['dict3']]
    
    print[dictA]
    print[dictB]
    print[dictC]
    

    Đầu ra bảng điều khiển

    {'key2': 'keyinfo2', 'key1': 'keyinfo'}
    {'k1': 'hot', 'k2': 'cross', 'k3': 'buns'}
    {'z': '3', 'y': '2', 'x': '1'}
    

    Nội dung của config.ini

    [dict1]
    key2 = keyinfo2
    key1 = keyinfo
    
    [dict2]
    k1 = hot
    k2 = cross
    k3 = buns
    
    [dict3]
    z = 3
    y = 2
    x = 1
    

    Làm thế nào để bạn đại diện cho một từ điển trong JSON?

    JSON là một cách thể hiện các mảng và từ điển của các giá trị [chuỗi, int, float, gấp đôi] dưới dạng tệp văn bản. Trong một tệp JSON, các mảng được ký hiệu là [] và từ điển được ký hiệu là {}.{ } .

    Làm cách nào để lưu từ điển trong Python?

    Bạn có thể lưu từ điển của mình vào một tệp văn bản bằng mã bên dưới:..
    # Xác định dict ..
    dict = {'python': '.py', 'c ++': '.cpp', 'java': '.java'}.
    # Mở tệp để viết ..
    # Viết tệp ..
    # Đóng tập tin..

    Từ điển trong Python giống như JSON?

    Một đối tượng JSON JSON rất giống với một từ điển Python.Một mảng json của người Viking rất giống với danh sách Python.Theo cùng một cách mà các đối tượng JSON có thể được lồng trong các mảng hoặc mảng được lồng trong các đối tượng, từ điển Python có thể được lồng trong danh sách hoặc danh sách được lồng trong từ điển.. A “JSON array” is very similar to a Python list. In the same way that JSON objects can be nested within arrays or arrays nested within objects, Python dictionaries can be nested within lists or lists nested within dictionaries.

    Làm cách nào để cứu JSON trong Python?

    Phương pháp 2: Viết JSON vào một tệp trong Python bằng json.dump [] Một cách khác để viết JSON vào một tệp là bằng cách sử dụng JSON.Phương thức Dump [] Gói JSON có chức năng Dump Dump, trực tiếp ghi từ điển vào một tệp dưới dạng JSON, mà không cần chuyển đổi nó thành một đối tượng JSON thực tế.json.dump[] Another way of writing JSON to a file is by using json. dump[] method The JSON package has the “dump” function which directly writes the dictionary to a file in the form of JSON, without needing to convert it into an actual JSON object.

    Bài Viết Liên Quan

    Chủ Đề