Làm việc với tệp json trong python

JSON là viết tắt của JavaScript Object Notation. Đó là một cú pháp đơn giản để lưu trữ dữ liệu theo cặp tên-giá trị. Các giá trị có thể là các loại dữ liệu khác nhau miễn là chúng hợp lệ. Các loại không được chấp nhận cho JSON bao gồm hàm, ngày tháng và

with open['user.json'] as user_file:
  file_contents = user_file.read[]
  
print[file_contents]
# {
#   "name": "John",
#   "age": 50,
#   "is_married": false,
#   "profession": null,
#   "hobbies": ["travelling", "photography"]
# }
7

Các tệp JSON được lưu trữ với phần mở rộng

with open['user.json'] as user_file:
  file_contents = user_file.read[]
  
print[file_contents]
# {
#   "name": "John",
#   "age": 50,
#   "is_married": false,
#   "profession": null,
#   "hobbies": ["travelling", "photography"]
# }
8 có cấu trúc JSON hợp lệ

Đây là cấu trúc của một tệp JSON trông như thế nào

{
  "name": "John",
  "age": 50,
  "is_married": false,
  "profession": null,
  "hobbies": ["traveling", "photography"]
}

Bạn sẽ thường sử dụng JSON để gửi và nhận dữ liệu từ máy chủ trong các ứng dụng web

Khi nhận được dữ liệu, chương trình sẽ đọc và phân tích cú pháp JSON để trích xuất dữ liệu cụ thể. Các ngôn ngữ khác nhau có phương pháp riêng để thực hiện việc này. Chúng ta sẽ xem cách thực hiện những điều này trong Python tại đây

Cách đọc tệp JSON

Giả sử JSON trong khối mã ở trên được lưu trữ trong tệp

with open['user.json'] as user_file:
  file_contents = user_file.read[]
  
print[file_contents]
# {
#   "name": "John",
#   "age": 50,
#   "is_married": false,
#   "profession": null,
#   "hobbies": ["travelling", "photography"]
# }
9. Sử dụng hàm có sẵn
with open['user.json'] as user_file:
  file_contents = user_file.read[]
  
print[file_contents]
# {
#   "name": "John",
#   "age": 50,
#   "is_married": false,
#   "profession": null,
#   "hobbies": ["travelling", "photography"]
# }
0 trong Python, chúng ta có thể đọc tệp đó và gán nội dung cho một biến. Đây là cách

with open['user.json'] as user_file:
  file_contents = user_file.read[]
  
print[file_contents]
# {
#   "name": "John",
#   "age": 50,
#   "is_married": false,
#   "profession": null,
#   "hobbies": ["travelling", "photography"]
# }

Bạn chuyển đường dẫn tệp tới phương thức

with open['user.json'] as user_file:
  file_contents = user_file.read[]
  
print[file_contents]
# {
#   "name": "John",
#   "age": 50,
#   "is_married": false,
#   "profession": null,
#   "hobbies": ["travelling", "photography"]
# }
1 để mở tệp và gán dữ liệu luồng từ tệp cho biến
with open['user.json'] as user_file:
  file_contents = user_file.read[]
  
print[file_contents]
# {
#   "name": "John",
#   "age": 50,
#   "is_married": false,
#   "profession": null,
#   "hobbies": ["travelling", "photography"]
# }
2. Sử dụng phương thức
with open['user.json'] as user_file:
  file_contents = user_file.read[]
  
print[file_contents]
# {
#   "name": "John",
#   "age": 50,
#   "is_married": false,
#   "profession": null,
#   "hobbies": ["travelling", "photography"]
# }
0, bạn có thể chuyển nội dung văn bản của tệp vào biến
with open['user.json'] as user_file:
  file_contents = user_file.read[]
  
print[file_contents]
# {
#   "name": "John",
#   "age": 50,
#   "is_married": false,
#   "profession": null,
#   "hobbies": ["travelling", "photography"]
# }
1

Tôi đã sử dụng

with open['user.json'] as user_file:
  file_contents = user_file.read[]
  
print[file_contents]
# {
#   "name": "John",
#   "age": 50,
#   "is_married": false,
#   "profession": null,
#   "hobbies": ["travelling", "photography"]
# }
2 ở đầu biểu thức để sau khi đọc nội dung của tệp, Python có thể đóng tệp

with open['user.json'] as user_file:
  file_contents = user_file.read[]
  
print[file_contents]
# {
#   "name": "John",
#   "age": 50,
#   "is_married": false,
#   "profession": null,
#   "hobbies": ["travelling", "photography"]
# }
1 hiện chứa phiên bản được xâu chuỗi của JSON. Bước tiếp theo, bây giờ bạn có thể phân tích cú pháp JSON

Cách phân tích cú pháp JSON

Python có các mô-đun dựng sẵn cho các hoạt động khác nhau. Để quản lý tệp JSON, Python có mô-đun

with open['user.json'] as user_file:
  file_contents = user_file.read[]
  
print[file_contents]
# {
#   "name": "John",
#   "age": 50,
#   "is_married": false,
#   "profession": null,
#   "hobbies": ["travelling", "photography"]
# }
4

Mô-đun này đi kèm với nhiều phương pháp. Một trong số đó là phương pháp

with open['user.json'] as user_file:
  file_contents = user_file.read[]
  
print[file_contents]
# {
#   "name": "John",
#   "age": 50,
#   "is_married": false,
#   "profession": null,
#   "hobbies": ["travelling", "photography"]
# }
5 để phân tích chuỗi JSON. Sau đó, bạn có thể gán dữ liệu được phân tích cú pháp cho một biến như thế này

________số 8

Sử dụng phương pháp

with open['user.json'] as user_file:
  file_contents = user_file.read[]
  
print[file_contents]
# {
#   "name": "John",
#   "age": 50,
#   "is_married": false,
#   "profession": null,
#   "hobbies": ["travelling", "photography"]
# }
5, bạn có thể thấy rằng biến
with open['user.json'] as user_file:
  file_contents = user_file.read[]
  
print[file_contents]
# {
#   "name": "John",
#   "age": 50,
#   "is_married": false,
#   "profession": null,
#   "hobbies": ["travelling", "photography"]
# }
7 hiện có một từ điển hợp lệ. Từ từ điển này, bạn có thể truy cập các khóa và giá trị trong đó

Cũng lưu ý cách

with open['user.json'] as user_file:
  file_contents = user_file.read[]
  
print[file_contents]
# {
#   "name": "John",
#   "age": 50,
#   "is_married": false,
#   "profession": null,
#   "hobbies": ["travelling", "photography"]
# }
8 từ JSON được chuyển đổi thành
with open['user.json'] as user_file:
  file_contents = user_file.read[]
  
print[file_contents]
# {
#   "name": "John",
#   "age": 50,
#   "is_married": false,
#   "profession": null,
#   "hobbies": ["travelling", "photography"]
# }
9 trong python. Điều này là do
with open['user.json'] as user_file:
  file_contents = user_file.read[]
  
print[file_contents]
# {
#   "name": "John",
#   "age": 50,
#   "is_married": false,
#   "profession": null,
#   "hobbies": ["travelling", "photography"]
# }
8 không hợp lệ trong
import json

with open['user.json'] as user_file:
  file_contents = user_file.read[]
  
print[file_contents]

parsed_json = json.loads[file_contents]
# {
#   'name': 'John',
#   'age': 50,
#   'is_married': False,
#   'profession': None,
#   'hobbies': ['travelling', 'photography']
# }
1

Cách sử dụng
import json

with open['user.json'] as user_file:
  file_contents = user_file.read[]
  
print[file_contents]

parsed_json = json.loads[file_contents]
# {
#   'name': 'John',
#   'age': 50,
#   'is_married': False,
#   'profession': None,
#   'hobbies': ['travelling', 'photography']
# }
2 để đọc và phân tích tệp JSON

Mô-đun

with open['user.json'] as user_file:
  file_contents = user_file.read[]
  
print[file_contents]
# {
#   "name": "John",
#   "age": 50,
#   "is_married": false,
#   "profession": null,
#   "hobbies": ["travelling", "photography"]
# }
4 cũng có phương thức
import json

with open['user.json'] as user_file:
  file_contents = user_file.read[]
  
print[file_contents]

parsed_json = json.loads[file_contents]
# {
#   'name': 'John',
#   'age': 50,
#   'is_married': False,
#   'profession': None,
#   'hobbies': ['travelling', 'photography']
# }
4 mà bạn có thể sử dụng để đọc đối tượng tệp và phân tích cú pháp cùng lúc. Sử dụng phương pháp này, bạn có thể cập nhật mã trước đó thành mã này

with open['user.json'] as user_file:
  file_contents = user_file.read[]
  
print[file_contents]
# {
#   "name": "John",
#   "age": 50,
#   "is_married": false,
#   "profession": null,
#   "hobbies": ["travelling", "photography"]
# }
8

Thay vì sử dụng phương thức

with open['user.json'] as user_file:
  file_contents = user_file.read[]
  
print[file_contents]
# {
#   "name": "John",
#   "age": 50,
#   "is_married": false,
#   "profession": null,
#   "hobbies": ["travelling", "photography"]
# }
0 của đối tượng tệp và sử dụng phương thức
import json

with open['user.json'] as user_file:
  file_contents = user_file.read[]
  
print[file_contents]

parsed_json = json.loads[file_contents]
# {
#   'name': 'John',
#   'age': 50,
#   'is_married': False,
#   'profession': None,
#   'hobbies': ['travelling', 'photography']
# }
6 của mô-đun
with open['user.json'] as user_file:
  file_contents = user_file.read[]
  
print[file_contents]
# {
#   "name": "John",
#   "age": 50,
#   "is_married": false,
#   "profession": null,
#   "hobbies": ["travelling", "photography"]
# }
4, bạn có thể trực tiếp sử dụng phương thức
import json

with open['user.json'] as user_file:
  file_contents = user_file.read[]
  
print[file_contents]

parsed_json = json.loads[file_contents]
# {
#   'name': 'John',
#   'age': 50,
#   'is_married': False,
#   'profession': None,
#   'hobbies': ['travelling', 'photography']
# }
4 để đọc và phân tích cú pháp đối tượng tệp

kết thúc

Dữ liệu JSON thường được biết đến với cấu trúc đơn giản và phổ biến [là tiêu chuẩn trong hầu hết các trường hợp] để trao đổi thông tin giữa máy chủ và máy khách

Các ngôn ngữ và công nghệ khác nhau có thể đọc và phân tích các tệp JSON theo những cách khác nhau. Trong bài viết này, chúng ta đã học cách đọc các tệp JSON và phân tích cú pháp các tệp đó bằng cách sử dụng phương thức

with open['user.json'] as user_file:
  file_contents = user_file.read[]
  
print[file_contents]
# {
#   "name": "John",
#   "age": 50,
#   "is_married": false,
#   "profession": null,
#   "hobbies": ["travelling", "photography"]
# }
0 của các đối tượng tệp và các phương thức
import json

with open['user.json'] as user_file:
  file_contents = user_file.read[]
  
print[file_contents]

parsed_json = json.loads[file_contents]
# {
#   'name': 'John',
#   'age': 50,
#   'is_married': False,
#   'profession': None,
#   'hobbies': ['travelling', 'photography']
# }
6 và
import json

with open['user.json'] as user_file:
  file_contents = user_file.read[]
  
print[file_contents]

parsed_json = json.loads[file_contents]
# {
#   'name': 'John',
#   'age': 50,
#   'is_married': False,
#   'profession': None,
#   'hobbies': ['travelling', 'photography']
# }
4 của mô-đun
with open['user.json'] as user_file:
  file_contents = user_file.read[]
  
print[file_contents]
# {
#   "name": "John",
#   "age": 50,
#   "is_married": false,
#   "profession": null,
#   "hobbies": ["travelling", "photography"]
# }
4

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO

Dillion Megida

Người ủng hộ nhà phát triển và Người sáng tạo nội dung đam mê chia sẻ kiến ​​thức của tôi về Công nghệ. Tôi dạy JavaScript / ReactJS / NodeJS / React Frameworks / TypeScript / et al

Nếu bạn đọc đến đây, hãy tweet cho tác giả để cho họ thấy bạn quan tâm. Tweet một lời cảm ơn

Học cách viết mã miễn phí. Chương trình giảng dạy mã nguồn mở của freeCodeCamp đã giúp hơn 40.000 người có được việc làm với tư cách là nhà phát triển. Bắt đầu

Làm cách nào để sử dụng tệp JSON trong Python?

Python có một gói tích hợp gọi là json, có thể được sử dụng để làm việc với dữ liệu JSON. Nó được thực hiện bằng cách sử dụng mô-đun JSON, cung cấp cho chúng ta rất nhiều phương thức trong số các phương thức loading[] và load[] sẽ giúp chúng ta đọc tệp JSON. . Giải tuần tự hóa JSON

Làm cách nào để lấy dữ liệu từ tệp JSON trong Python?

Sử dụng phương thức read, bạn có thể chuyển nội dung văn bản của tệp vào biến file_contents. Tôi đã sử dụng with ở đầu biểu thức để sau khi đọc nội dung của tệp, Python có thể đóng tệp. file_contents hiện chứa phiên bản được xâu chuỗi của JSON

Làm cách nào để đọc nội dung của tệp JSON trong Python?

Ở đây, chúng tôi đã sử dụng hàm open[] để đọc tệp json. Sau đó, tệp được phân tích cú pháp bằng json. phương thức load[] cung cấp cho chúng ta một từ điển có tên data

Cách tốt nhất để làm việc với JSON trong Python là gì?

Bắt đầu bằng cách nhập thư viện json. Chúng ta sử dụng hàm open để đọc tệp JSON và sau đó là phương thức json. load[] để phân tích cú pháp chuỗi JSON thành một từ điển Python có tên superHeroSquad . Đó là nó.

Chủ Đề