簡體   English   中英

通過Python的內存大小獲取字符數

[英]Getting number of characters through it's memory size in python

[Python]

我有一個字符串,我知道它在內存中的大小。 我想獲取其中包含的字符數(粗略估計)。

實際情況下,我想通過郵件發送報告,郵件的內容超出了允許的大小。 我想根據最大大小將郵件拆分為多個。 但是我沒有辦法將最大大小與字符串中的字符數相關聯。

import smtplib

smtp = smtplib.SMTP('server.name')
smtp.ehlo()
max_size = smtp.esmtp_features['size']

message_data = <some string data exceeding the max_size>
# Now, how can I get the number of characters in message_data which exceeds the max_szie

謝謝,

字符串中的字符數是內存中的大小(必須以字節為單位),您必須減去37(python 2.7 / mac os)

import sys

def estimate_chars():
    "size in bytes"
    s = ""
    for idx in range(100):
        print idx * 10, sys.getsizeof(s), len(s)
        s += '1234567890'

estimate_chars()

結果:(字符|字節| len)

0 37 0
10 47 10
20 57 20
30 67 30
40 77 40
50 87 50
...
960 997 960
970 1007 970
980 1017 980
990 1027 990

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM