utf 8 - split utf-8 string into bytes in python -


i trying split utf-8 string bytes in python 3. problem is, when use bytearray, byte, encode etc functions array size of element 14 bytes, not 1 byte expected. need split text file sequence of bytes , send them byte after byte using sockets. tried this:

infile = open (file, "r") str = infile.read() byte_str = bytes(str, 'utf-8') print("size of byte_str",sys.getsizeof(byte_str[0])) 

print gives me 14, need 1... suggestion?

quoting official documentation:

sys.getsizeof(object[, default]) 

return size of object in bytes. object can type of object. built-in objects return correct results, not have hold true third-party extensions implementation specific.

only memory consumption directly attributed object accounted for, not memory consumption of objects refers to.

if given, default returned if object not provide means retrieve size. otherwise typeerror raised.

getsizeof() calls object’s __sizeof__ method , adds additional garbage collector overhead if object managed garbage collector.

see recursive sizeof recipe example of using getsizeof() recursively find size of containers , contents.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -