Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Pretty print Python dictionary từ command line

Dùng pprint module hoặc json.dumps(indent=4).

pprint

import pprint
a = {'bar': 22, 'foo': 45}
pprint.pprint(a, width=10)

Kết quả: {'bar': 22, 'foo': 45}

json.dumps (đẹp hơn)

import json
a = {'bar': 22, 'foo': 45}
print(json.dumps(a, indent=4))

Kết quả:

{
    "bar": 22,
    "foo": 45
}