Xem thảo luận
Cải thiện bài viết
Lưu bài viết
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]
4import 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]
5Con 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]
2import 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]
3Ví 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]
8import 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]
0import 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
9import
0import
1import
2import
3import
4import
5import 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]
9import
7Output:
Để 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