Liệt kê vào tệp json python

Chuyển đến nội dung chính

Python trung gian

Nâng cao kỹ năng khoa học dữ liệu của bạn bằng cách tạo trực quan hóa bằng Matplotlib và thao tác với DataFrames bằng gấu trúc

Có liên quan

Dữ liệu văn bản trong Python Cheat Sheet

Chào mừng bạn đến với bảng gian lận của chúng tôi để làm việc với dữ liệu văn bản trong Python. Chúng tôi đã biên soạn một danh sách các hàm và gói hữu ích nhất để dọn dẹp, xử lý và phân tích dữ liệu văn bản trong Python, cùng với các ví dụ và giải thích rõ ràng, vì vậy bạn sẽ có mọi thứ cần biết về cách làm việc với dữ liệu văn bản trong Python.

Hướng dẫn về tập hợp và lý thuyết tập hợp trong Python

Tìm hiểu về bộ Python. chúng là gì, cách tạo chúng, khi nào sử dụng chúng, các chức năng tích hợp và mối quan hệ của chúng với các hoạt động lý thuyết tập hợp

Hướng dẫn về gấu trúc. Khung dữ liệu trong Python

Khám phá phân tích dữ liệu với Python. Pandas DataFrames giúp thao tác dữ liệu của bạn dễ dàng, từ việc chọn hoặc thay thế các cột và chỉ mục để định hình lại dữ liệu của bạn

Xem ThêmXem Thêm

Tham khảo các bài viết sau để biết cách đọc JSON từ file và ghi JSON vào file trong Python

2. Nối vào một mảng JSON bằng Python List

Tệp

import json
from os import path
 
filename = 'c:/temp/users.json'
listObj = []
 
# Check if file exists
if path.isfile[filename] is False:
  raise Exception["File not found"]
 
# Read JSON file
with open[filename] as fp:
  listObj = json.load[fp]
 
# Verify existing list
print[listObj]
print[type[listObj]]
 
listObj.append[{
  "Name": "Person_3",
  "Age": 33,
  "Email": "33@gmail.com"
}]
 
# Verify updated list
print[listObj]
 
with open[filename, 'w'] as json_file:
    json.dump[listObj, json_file, 
                        indent=4,  
                        separators=[',',': ']]
 
print['Successfully appended to the JSON file']
3 có danh sách 2 người dùng. Chúng tôi sẽ thêm một người dùng thứ ba vào nó

[
    {
        "Name": "Person_1",
        "Age": 11,
        "Email": "11@gmail.com"
    },
    {
        "Name": "Person_2",
        "Age": 22,
        "Email": "22@gmail.com"
    }
]
import json
from os import path
 
filename = 'c:/temp/users.json'
listObj = []
 
# Check if file exists
if path.isfile[filename] is False:
  raise Exception["File not found"]
 
# Read JSON file
with open[filename] as fp:
  listObj = json.load[fp]
 
# Verify existing list
print[listObj]
print[type[listObj]]
 
listObj.append[{
  "Name": "Person_3",
  "Age": 33,
  "Email": "33@gmail.com"
}]
 
# Verify updated list
print[listObj]
 
with open[filename, 'w'] as json_file:
    json.dump[listObj, json_file, 
                        indent=4,  
                        separators=[',',': ']]
 
print['Successfully appended to the JSON file']

Tệp JSON được cập nhật là

[
    {
        "Name": "Person_1",
        "Age": 11,
        "Email": "11@gmail.com"
    },
    {
        "Name": "Person_2",
        "Age": 22,
        "Email": "22@gmail.com"
    },
    {
        "Name": "Person_3",
        "Age": 33,
        "Email": "33@gmail.com"
    }
]

3. Nối vào đối tượng JSON bằng Python Dictionary

Tệp

import json
from os import path
 
filename = 'c:/temp/users.json'
listObj = []
 
# Check if file exists
if path.isfile[filename] is False:
  raise Exception["File not found"]
 
# Read JSON file
with open[filename] as fp:
  listObj = json.load[fp]
 
# Verify existing list
print[listObj]
print[type[listObj]]
 
listObj.append[{
  "Name": "Person_3",
  "Age": 33,
  "Email": "33@gmail.com"
}]
 
# Verify updated list
print[listObj]
 
with open[filename, 'w'] as json_file:
    json.dump[listObj, json_file, 
                        indent=4,  
                        separators=[',',': ']]
 
print['Successfully appended to the JSON file']
3 có danh sách 2 người dùng. Chúng tôi sẽ thêm vai trò thuộc tính mới và sửa đổi thuộc tính hiện có Tuổi

{
	"Name": "Person_1",
	"Age": 11,
	"Email": "11@gmail.com"
}
import json
from os import path
 
filename = 'c:/temp/users.json'
dictObj = []
 
# Check if file exists
if path.isfile[filename] is False:
  raise Exception["File not found"]
 
# Read JSON file
with open[filename] as fp:
  dictObj = json.load[fp]
 
# Verify existing dict
print[dictObj]

print[type[dictObj]]
 
dictObj.update[{"Age": 12,"Role": "Developer"}]
 
# Verify updated dict
print[dictObj]
 
with open[filename, 'w'] as json_file:
    json.dump[dictObj, json_file, 
                        indent=4,  
                        separators=[',',': ']]
 
print['Successfully written to the JSON file']

Tệp JSON được cập nhật là

{
    "Name": "Person_1",
    "Age": 12,
    "Email": "11@gmail.com",
    "Role": "Developer"
}

4. Lỗi thuộc tính. đối tượng 'dict' không có thuộc tính 'chắp thêm'

Chúng tôi có thể gặp lỗi này nếu đối tượng JSON được đọc từ phương thức

import json
from os import path
 
filename = 'c:/temp/users.json'
listObj = []
 
# Check if file exists
if path.isfile[filename] is False:
  raise Exception["File not found"]
 
# Read JSON file
with open[filename] as fp:
  listObj = json.load[fp]
 
# Verify existing list
print[listObj]
print[type[listObj]]
 
listObj.append[{
  "Name": "Person_3",
  "Age": 33,
  "Email": "33@gmail.com"
}]
 
# Verify updated list
print[listObj]
 
with open[filename, 'w'] as json_file:
    json.dump[listObj, json_file, 
                        indent=4,  
                        separators=[',',': ']]
 
print['Successfully appended to the JSON file']
5 thuộc loại
[
    {
        "Name": "Person_1",
        "Age": 11,
        "Email": "11@gmail.com"
    },
    {
        "Name": "Person_2",
        "Age": 22,
        "Email": "22@gmail.com"
    }
]
2

Ví dụ đầu tiên đọc danh sách JSON

import json
from os import path
 
filename = 'c:/temp/users.json'
listObj = []
 
# Check if file exists
if path.isfile[filename] is False:
  raise Exception["File not found"]
 
# Read JSON file
with open[filename] as fp:
  listObj = json.load[fp]
 
# Verify existing list
print[listObj]
print[type[listObj]]
 
listObj.append[{
  "Name": "Person_3",
  "Age": 33,
  "Email": "33@gmail.com"
}]
 
# Verify updated list
print[listObj]
 
with open[filename, 'w'] as json_file:
    json.dump[listObj, json_file, 
                        indent=4,  
                        separators=[',',': ']]
 
print['Successfully appended to the JSON file']
7 nên đối tượng được tải thuộc loại
import json
from os import path
 
filename = 'c:/temp/users.json'
listObj = []
 
# Check if file exists
if path.isfile[filename] is False:
  raise Exception["File not found"]
 
# Read JSON file
with open[filename] as fp:
  listObj = json.load[fp]
 
# Verify existing list
print[listObj]
print[type[listObj]]
 
listObj.append[{
  "Name": "Person_3",
  "Age": 33,
  "Email": "33@gmail.com"
}]
 
# Verify updated list
print[listObj]
 
with open[filename, 'w'] as json_file:
    json.dump[listObj, json_file, 
                        indent=4,  
                        separators=[',',': ']]
 
print['Successfully appended to the JSON file']
0. Nếu chúng ta đang đọc một tệp có đối tượng JSON
import json
from os import path
 
filename = 'c:/temp/users.json'
listObj = []
 
# Check if file exists
if path.isfile[filename] is False:
  raise Exception["File not found"]
 
# Read JSON file
with open[filename] as fp:
  listObj = json.load[fp]
 
# Verify existing list
print[listObj]
print[type[listObj]]
 
listObj.append[{
  "Name": "Person_3",
  "Age": 33,
  "Email": "33@gmail.com"
}]
 
# Verify updated list
print[listObj]
 
with open[filename, 'w'] as json_file:
    json.dump[listObj, json_file, 
                        indent=4,  
                        separators=[',',': ']]
 
print['Successfully appended to the JSON file']
9 thì đối tượng được tải sẽ thuộc loại từ điển và đoạn mã trên sẽ đưa ra AttributeError trong khi gọi các thao tác danh sách

Làm cách nào để chuyển đổi danh sách thành tệp JSON trong Python?

Để chuyển đổi Danh sách Python thành JSON, hãy sử dụng json. hàm kết xuất[] . Hàm dumps[] lấy danh sách làm đối số và trả về Chuỗi JSON.

Làm cách nào để chuyển đổi danh sách thành mảng JSON trong Python?

Nếu bạn có một đối tượng Python, bạn có thể chuyển đổi nó thành một chuỗi JSON bằng cách sử dụng hàm json. phương thức bãi []. .
tuple
sợi dây
trôi nổi
Sai

Làm cách nào để chuyển dữ liệu danh sách trong JSON?

chuyển Danh sách cùng với dữ liệu Json làm kiểu trả về .
công khai JsonResult GetByKey[int? id]
List message = new List[];.
thông điệp. Thêm["khu1"];
thông điệp. Thêm["khu2"];
thông điệp. Thêm["khu3"];
dữ liệu var = [từ z trong db. Công tyMô hình
ở đâu z. FirmId == id

Làm cách nào để chuyển đổi danh sách từ điển thành JSON trong Python?

dumps[] Hàm này sẽ chuyển đổi danh sách từ điển thành JSON. Thông số. từ điển – tên của một từ điển sẽ được chuyển đổi thành đối tượng JSON.

Chủ Đề