Computer >> Hướng Dẫn Máy Tính >  >> Hệ Thống >> Linux

Cách pretty print file JSON trong Python

Giống nhiều ngôn ngữ lập trình khác, Python làm việc tốt với data JavaScript Object Notation (JSON). Có thể pull data từ vài source và xử lý như file bình thường. Hữu ích khi muốn pretty print file JSON trong Python.

Bài viết chỉ cách pretty print file JSON 2 cách. Cả hai dùng Terminal và command line, tuy nhiên có thể không cần kiến thức Python cho một trong hai.

Pretty Print là gì (Và tại sao dùng với JSON file)

Ngắn gọn: pretty print lấy data raw format và biến thành readable. Nói cách khác, làm cho nó "pretty".

Thông thường, bạn làm điều này với file pull vào program cần parsed bởi human. Thường, data từ format khác (như JSON) đã "minified". Loại bỏ toàn bộ whitespace để lean cho computer parse.

Ví dụ: lấy raw database data minified chạy qua script thêm indentation và formatting đúng. Từ đó export cho người khác và làm việc như ý.

Cách pretty print file JSON trong Python

Dưới đây, chỉ cách pretty print file JSON trong Python. Ví dụ dùng NeoVim và built-in :term. Tuy nhiên, không lý do gì không làm direct từ Terminal trong Python interpreter.

1. Định nghĩa JSON file

Đầu tiên, cần JSON data làm việc. Có 2 approach: define data trong Python script hoặc qua JSON file. Cá cược data đa số từ file, nên focus ở đây.

Trường hợp tác giả: dump JSON data zero formatting.

File này – addresses.json – đưa vào Python và format. Để làm việc JSON file trong Python, import module json. Sau đó define file như variable.

Ở điểm này, bắt đầu làm việc "addresses.json".

2. Mở JSON file dùng Python

Tiếp theo, mở "addresses.json". Qua method with open…as thông thường:

with open(json_file, 'r') as file: …

Trong block code này, gọi json.load() trên "addresses.json" và gán vào variable:

parsed = json.load(file)

Chạy file bây giờ, nó execute nhưng không thấy result trong Terminal. Cần bước 3.

3. Pretty Print JSON file (Và điều chỉnh settings)

Khía cạnh cuối: thực hiện print. Thêm dòng sau as indented line trong block with open…as:

print(json.dumps(parsed, indent=4, sort_keys=True))

Phân tích: print gọi json.dumps(). Áp dụng 3 argument:

  • Tên file muốn pretty print.
  • Kích thước indent.
  • Có sort JSON file theo keys không.

Khi sẵn sàng, save thay đổi, mở Terminal mới. Navigate đến project folder đúng, chạy program.

Terminal output áp dụng settings và pretty print JSON file.

Tất nhiên, thường store pretty JSON ở đâu đó thay vì print ra Terminal. Tuy nhiên, có one-liner snippet pretty print JSON file từ command line.

Cách pretty print file JSON trong Python từ command line

Cách siêu nhanh pretty print JSON file trong Python direct từ command line as Terminal output. Mở Terminal mới, navigate project folder, chạy:

cat addresses.json | python3 -m json.tool

Ví dụ này, đổi "addresses.json" thành filename của bạn. Chạy xong, Terminal hiển thị formatted output.

Giải pháp no-code tuyệt vời pretty print JSON file trong Python on-the-fly, mất vài giây chạy.

Kết thúc

Có nhiều use case pretty print JSON file trong Python. Thường muốn làm minified data readable, có thể export cho user khác. Bất kể lý do, Python cho phép làm này dùng json module và block code "with open…as".

Nếu quan tâm Python còn làm gì nữa, xem bài khác về Python looping và function range(). Phương pháp này giúp ích daily work của bạn như thế nào? Comment cho chúng tôi biết!