Hướng dẫn how do i get the size of a json string in python? - làm cách nào để lấy kích thước của chuỗi json trong python?

Trong blog này, bạn sẽ tìm hiểu cách tìm kích thước đối tượng chuỗi JSON bằng byte bằng phương thức encode() hoặc bytes() trong Python. Chúng tôi cũng có thể sử dụng các phương pháp này để tìm kích thước tải trọng API JSON REST trong Python.

Để tính toán kích thước, trước tiên chúng ta cần chuyển đổi các đối tượng Python thành các chuỗi JSON bằng cách sử dụng json.dumps().

import json 
dictionary = {"test_1":"xyz", "test_2":"zyx"} #python object json_string = json.dumps(dictionary)

Mã hóa đối tượng chuỗi thành đối tượng byte bằng phương thức encode("utf-8") hoặc bytes(json_string, "utf-8"). Điều này là do trực tiếp sử dụng phương pháp

byte_ = json_string.encode("utf-8")
# or byte_ = bytes(json_string, "utf-8")
print(byte_)
# output: b'{"test_1": "xyz", "test_2": "zyx"}'
0 sẽ cung cấp cho chúng ta số lượng cặp giá trị khóa. Ở đây, ví dụ,
byte_ = json_string.encode("utf-8")
# or byte_ = bytes(json_string, "utf-8")
print(byte_)
# output: b'{"test_1": "xyz", "test_2": "zyx"}'
1 cung cấp cho chúng ta 2 là đầu ra vì có hai cặp giá trị khóa.

Nếu bạn muốn biết về

byte_ = json_string.encode("utf-8")
# or byte_ = bytes(json_string, "utf-8")
print(byte_)
# output: b'{"test_1": "xyz", "test_2": "zyx"}'
2, hãy đọc bài viết này.

Phương thức mã hóa () mã hóa chuỗi. Nếu không có gì được chỉ định thì UTF-8 được sử dụng và tương tự áp dụng cho phương thức byte ().

byte_ = json_string.encode("utf-8")
# or byte_ = bytes(json_string, "utf-8")
print(byte_)
# output: b'{"test_1": "xyz", "test_2": "zyx"}'

Bây giờ, chúng ta có thể sử dụng phương pháp

byte_ = json_string.encode("utf-8")
# or byte_ = bytes(json_string, "utf-8")
print(byte_)
# output: b'{"test_1": "xyz", "test_2": "zyx"}'
0 để tìm độ dài.

size_in_bytes = len(byte_)
# output: 34 (in bytes)

Cảm ơn bạn đã đọc blog này! 😇

Tôi cần biết kích thước của các đối tượng trong chương trình Python của tôi. Tôi có vòng lặp

byte_ = json_string.encode("utf-8")
# or byte_ = bytes(json_string, "utf-8")
print(byte_)
# output: b'{"test_1": "xyz", "test_2": "zyx"}'
4 trong đó tôi cần biết kích thước của các đối tượng. Nếu tôi sử dụng
byte_ = json_string.encode("utf-8")
# or byte_ = bytes(json_string, "utf-8")
print(byte_)
# output: b'{"test_1": "xyz", "test_2": "zyx"}'
5 thì bộ nhớ không miễn phí ngay lập tức và tôi không thể thấy kích thước thực tế. Có cách nào để làm điều đó không?

Đối tượng của tôi là JSON và Chuỗi. Trong trường hợp xấu nhất, tôi có thể lưu chúng vào tệp và xem kích thước tệp. Nhưng làm thế nào tôi có thể nhìn vào kích thước tệp từ mã Python?

Chỉnh sửa: Kích thước của các đối tượng tuần tự hóa quan trọng hơn đối với tôi. Vì vậy, phần thứ hai của câu hỏi là điều cần thiết.: size of serialised objects is more important for me. so second part of the question is essential.

Thanks.

Hỏi ngày 30 tháng 4 năm 2012 lúc 16:57Apr 30, 2012 at 16:57

Ashimashimashim

23.5K29 Huy hiệu vàng71 Huy hiệu bạc94 Huy hiệu đồng29 gold badges71 silver badges94 bronze badges

10

Xem xét rằng

byte_ = json_string.encode("utf-8")
# or byte_ = bytes(json_string, "utf-8")
print(byte_)
# output: b'{"test_1": "xyz", "test_2": "zyx"}'
6 bạn có thể sử dụng
byte_ = json_string.encode("utf-8")
# or byte_ = bytes(json_string, "utf-8")
print(byte_)
# output: b'{"test_1": "xyz", "test_2": "zyx"}'
7 theo nghĩa đen.

Xem xét những điều sau đây

obj = {'content' : 'something goes here'}
json_obj = json.dumps(obj)
json_size = len(json_obj)

serialized_size = len(serialized_object)

if json_size < serialized_size:
  print "I'd use the JSON with this..."

Đã trả lời ngày 30 tháng 4 năm 2012 lúc 17:07Apr 30, 2012 at 17:07

Hướng dẫn how do i get the size of a json string in python? - làm cách nào để lấy kích thước của chuỗi json trong python?

Lukecampbelllukecampbelllukecampbell

14.4K4 Huy hiệu vàng33 Huy hiệu bạc32 Huy hiệu Đồng4 gold badges33 silver badges32 bronze badges

2

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    Đọcsys.getsizeof() can be done to find the storage size of a particular object that occupies some space in the memory. This function returns the size of the object in bytes. It takes at most two arguments i.e Object itself.

    Bàn luận Only the memory consumption directly attributed to the object is accounted for, not the memory consumption of objects it refers to.

    Examples:    

    Input: 
    # Any Integer Value
    sys.getsizeof(4) 
    
    Expected Output: 4 bytes (Size of integer is 4bytes)
    Actual Output: 28 bytes

    Trong Python, việc sử dụng sys.getsizeOf () có thể được thực hiện để tìm kích thước lưu trữ của một đối tượng cụ thể chiếm một số không gian trong bộ nhớ. Hàm này trả về kích thước của đối tượng trong byte. Nó mất nhiều nhất hai đối số tức là chính đối tượng.

    Lưu ý: Chỉ tiêu thụ bộ nhớ được quy cho trực tiếp cho đối tượng được tính, không phải mức tiêu thụ bộ nhớ của các đối tượng mà nó đề cập đến.Ở đây, cách thức chúng ta có thể diễn giải đầu ra thực tế. Có một cái nhìn vào bảng dưới đây: & nbsp;Loại đối tượng
    Kích thước thực sự28 Ghi chú
    int49 Na
    str+1 mỗi ký tự bổ sung (49+tổng chiều dài của ký tự)Tuple
    40 (Tuple trống)+8 mỗi mục bổ sung trong một tuple (40 + 8*Tổng chiều dài của ký tự)danh sách
    56 (Danh sách trống)216 +8 mỗi mục bổ sung trong danh sách (56 + 8*Tổng chiều dài của ký tự)
    bộ232 0-4 Lấy kích thước 216. 5-19 Lấy kích thước 728. 20 sẽ mất 2264, v.v.
    DIGN136 0-5 có kích thước 232. Kích thước 6-10 sẽ là 360. 11th sẽ mất 640, v.v.

    Example:

    Python3

    func def

    Không có thuộc tính và đối số mặc định

    size_in_bytes = len(byte_)
    # output: 34 (in bytes)
    5
    size_in_bytes = len(byte_)
    # output: 34 (in bytes)
    6

    byte_ = json_string.encode("utf-8")
    # or byte_ = bytes(json_string, "utf-8")
    print(byte_)
    # output: b'{"test_1": "xyz", "test_2": "zyx"}'
    8
    byte_ = json_string.encode("utf-8")
    # or byte_ = bytes(json_string, "utf-8")
    print(byte_)
    # output: b'{"test_1": "xyz", "test_2": "zyx"}'
    9

    size_in_bytes = len(byte_)
    # output: 34 (in bytes)
    5
    obj = {'content' : 'something goes here'}
    json_obj = json.dumps(obj)
    json_size = len(json_obj)
    
    serialized_size = len(serialized_object)
    
    if json_size < serialized_size:
      print "I'd use the JSON with this..."
    
    3

    size_in_bytes = len(byte_)
    # output: 34 (in bytes)
    0____21
    size_in_bytes = len(byte_)
    # output: 34 (in bytes)
    2
    size_in_bytes = len(byte_)
    # output: 34 (in bytes)
    3
    size_in_bytes = len(byte_)
    # output: 34 (in bytes)
    4

    size_in_bytes = len(byte_)
    # output: 34 (in bytes)
    5
    Input: 
    # Any Integer Value
    sys.getsizeof(4) 
    
    Expected Output: 4 bytes (Size of integer is 4bytes)
    Actual Output: 28 bytes
    8

    size_in_bytes = len(byte_)
    # output: 34 (in bytes)
    7
    size_in_bytes = len(byte_)
    # output: 34 (in bytes)
    1
    size_in_bytes = len(byte_)
    # output: 34 (in bytes)
    2
    obj = {'content' : 'something goes here'}
    json_obj = json.dumps(obj)
    json_size = len(json_obj)
    
    serialized_size = len(serialized_object)
    
    if json_size < serialized_size:
      print "I'd use the JSON with this..."
    
    0
    size_in_bytes = len(byte_)
    # output: 34 (in bytes)
    4

    size_in_bytes = len(byte_)
    # output: 34 (in bytes)
    5bytes()3

    Các

    size_in_bytes = len(byte_)
    # output: 34 (in bytes)
    5json.dumps()6

    Input: 
    # Any Integer Value
    sys.getsizeof(4) 
    
    Expected Output: 4 bytes (Size of integer is 4bytes)
    Actual Output: 28 bytes
    9
    size_in_bytes = len(byte_)
    # output: 34 (in bytes)
    1 encode()1
    obj = {'content' : 'something goes here'}
    json_obj = json.dumps(obj)
    json_size = len(json_obj)
    
    serialized_size = len(serialized_object)
    
    if json_size < serialized_size:
      print "I'd use the JSON with this..."
    
    7
    obj = {'content' : 'something goes here'}
    json_obj = json.dumps(obj)
    json_size = len(json_obj)
    
    serialized_size = len(serialized_object)
    
    if json_size < serialized_size:
      print "I'd use the JSON with this..."
    
    8
    obj = {'content' : 'something goes here'}
    json_obj = json.dumps(obj)
    json_size = len(json_obj)
    
    serialized_size = len(serialized_object)
    
    if json_size < serialized_size:
      print "I'd use the JSON with this..."
    
    9
    obj = {'content' : 'something goes here'}
    json_obj = json.dumps(obj)
    json_size = len(json_obj)
    
    serialized_size = len(serialized_object)
    
    if json_size < serialized_size:
      print "I'd use the JSON with this..."
    
    8
    obj = {'content' : 'something goes here'}
    json_obj = json.dumps(obj)
    json_size = len(json_obj)
    
    serialized_size = len(serialized_object)
    
    if json_size < serialized_size:
      print "I'd use the JSON with this..."
    
    9
    obj = {'content' : 'something goes here'}
    json_obj = json.dumps(obj)
    json_size = len(json_obj)
    
    serialized_size = len(serialized_object)
    
    if json_size < serialized_size:
      print "I'd use the JSON with this..."
    
    8__

    size_in_bytes = len(byte_)
    # output: 34 (in bytes)
    5bytes(json_string, "utf-8")7

    Output:

    Hướng dẫn how do i get the size of a json string in python? - làm cách nào để lấy kích thước của chuỗi json trong python?

    JSON tính toán kích thước như thế nào?

    Sử dụng Đo bước () để tính toán số lượng ký tự sẽ được in bằng printto ().Sử dụng ĐoPrettyLpm () để tính toán số lượng ký tự sẽ được in bởi PrettyPrintTo (). . Use measurePrettyLength() to compute the number of characters that will be printed by prettyPrintTo() .

    Một chuỗi JSON là bao nhiêu byte?

    Json nhị phân phổ quát.

    Có bao nhiêu byte là một nhân vật trong JSON?

    Loại char trong Universal nhị phân JSON là một byte không dấu có nghĩa là đại diện cho một ký tự ASCII có thể in duy nhất (Giá trị thập phân 0-127).Nói cách khác, loại char đại diện cho một ký tự được mã hóa UTF-8 đơn byte.unsigned byte meant to represent a single printable ASCII character (decimal values 0-127). Put another way, the char type represents a single-byte UTF-8 encoded character.