Count bytes in python

How do i get the byte count of a variable in python just like wc -c gives in unix

Question: i am facing some problem with files with huge data. For that, you can use : Solution 4: gives you the size in bytes if it’s binary data.

How do i get the byte count of a variable in python just like wc -c gives in unix

i am facing some problem with files with huge data. i need to skip doing some execution on those files. i get the data of the file into a variable. now i need to get the byte of the variable and if it is greater than 102400 , then print a message.

update : i cannot open the files , since it is present in a tar file. the content is already getting copied to a variable called ‘data’ i am able to print contents of the variable data. i just need to check if it has more than 102400 bytes.

import os length_in_bytes = os.stat('file.txt').st_size if length_in_bytes > 102400: print 'Its a big file!' 

Update to work on files in a tarfile

import tarfile tf = tarfile.TarFile('foo.tar') for member in tarfile.getmembers(): if member.size > 102400: print 'It's a big file in a tarfile - the file is called %s!' % member.name 

Just check the length of the string, then:

if ****(data) > 102400: print "Skipping file which is too large, at %d bytes" % ****(data) else: process(data) # The normal processing 

If I’m understanding the question correctly, you want to skip certain input files if they’re too large. For that, you can use os.path.getsize() :

import os.path if os.path.getsize('f')  

****(data) gives you the size in bytes if it's binary data. With strings the size depends on the encoding used.

Declaring a single byte variable in Python, Python doesn't differentiate between characters and strings the way C does, nor does it care about int bit widths. For a single byte, you basically have three choices: A length 1 bytes (or bytearray) object mychar = b'\xff' (or mychar = bytearray (b'\xff'))

How to get tensorflow Tensor size in bytes?

Let's say I have a very simple variable:

my_var = tf.get_variable("my_var", (100,)) 

I would like to be able to compute the size (not shape) in bytes of the Tensor. Naturally it depends on the dtype.

I haven't found any way in doing in the official documentation.

You can compute the size in bytes of the Tensor using slim.

import tensorflow as tf from tensorflow.contrib import slim my_var = tf.get_variable("my_var", (100,)) slim.model_analyzer.analyze_vars([my_var], print_info=True) 
--------- Variables: name (type shape) [size] --------- my_var:0 (float32_ref 100) [100, bytes: 400] Total size of variables: 100 Total bytes of variables: 400 

You can get more information of course in the slim github. Have fun!

Python - How to get tensorflow Tensor size in bytes?, Let's say I have a very simple variable: my_var = tf.get_variable("my_var", (100,)) I would like to be able to compute the size (not shape) in bytes of the Tensor. Naturally it depends on the dtype. I haven't found any way in doing in the official documentation.

Python boolean byte size [duplicate]

Apparently integers costs 24 bytes in Python. I can understand that it does so because of extra bells and whistles of representing unbounded number. However it looks like boolean data types also cost whooping 24 bytes even though it might ever represent only two values. Why?

Edit: I'm not asking for best way to store bools. I'm already aware of NumPy, BitArray etc from other answers. My question is why , not how . Just to be clear and focused about that I've removed 2nd part of the question.

A bool may be pretty huge for what it represents, but there are only two of them. A list full of True s only contains 4- or 8-byte references to the one canonical True object.

If 8 bytes is still too big, and you really want to use Python for whatever it is you're doing, you could consider using an array type like that provided by the built-in array module or NumPy. These offer 1-byte-per-bool representations. If this is still too much, you could use a bitset, either manually with Python's built-in bignums or with something like BitVector from PyPI. These options are likely to slow your program way down. Some of them can offer speed improvements, but only if you take advantage of features that let you push work out of interpreted code and into C.

How big can variable be, in python?, The largest positive integer supported by the platform’s Py_ssize_t type, and thus the maximum size lists, strings, dicts, and many other containers can have. On my MacBook Pro with a 64-bit build of CPython, it's quite sensibly 2 63 -1 bytes: >>> import sys >>> sys.maxsize 9223372036854775807 >>>. While on my …

How to get the size of struct in python if the data inside struct has variable length binary string?

This code will read data from block device and pack data using struct.pack() and again unpack data using struct.unpack() . This is part of my main socket program. But I am facing an issue in calculating struct size . So I am putting a small code here to demonstrate my problem.

import sys,os import struct as st dev_read_data = os.open("/dev/sdc1",os.O_RDONLY) buffer_size = 230400 offset = 0 while True: data = os.pread(dev_read_data,buffer_size,offset) packed_data = st.pack("%dsi" %(****(data)),data,offset) # This statement packs data and assign it to packed_data. Till here code works fine. print ('Packed_Data <]'.format(packed_data)) unpacked_data = st.unpack("%dsi" %(****(data)),packed_data) # This unpacks data successfully. offset += buffer_size if buffer_size == 10036977152: break 

Now I want to calculate the size of struct using function:

But in this case only one parameter can be passed. So how to get struct size in case of variable length binary string?

I will be very thankful if experts can find some time to answer my query.

import os import binascii import zlib path = "/dev/sdc1" stat = os.stat(path) block_SIZE = stat.st_blksize block_COUNT = os.statvfs(path).f_blocks image = file(path,"rb") indicator = 0 while True : try : if indicator > 2 : break #if indicator > block_Count : break image.seek(indicator*block_SIZE) data = image.read(block_SIZE) HEX_CRC32 = binascii.unhexlify(str(hex(zlib.crc32(data) & 0xffffffff))[2:]) header = binascii.unhexlify(("%010X" % indicator)+("%04x"%block_SIZE)) """ NOW SEND TO SOCKET My_socket.write(header+data+HEX_CRC32) check Frame number : first 5 byte Data Length : 2 Byte (block_SIZE) Data : Data content CRC32 : 2 Byte (32-bit value) Client side : Check CRC32 , get `data[7:-2]` for current block """ indicator += 1 except Exception,e : """ very important (if failed,re send packet ) """ print e 

if indicator > block_Count : break

Not only brake ! Tell to client all packet has ben succesfully send, please close to socket on client side.

Tar - how do i get the byte count of a variable in python, i am facing some problem with files with huge data. i need to skip doing some execution on those files. i get the data of the file into a variable. now i need to get the byte of the variable and if it is greater than 102400 , then print a message. update : i cannot open the files , since it is present in a tar file. the …

Источник

Count bytes in python

Last updated: Feb 18, 2023
Reading time · 3 min

banner

# Table of Contents

# Get the length of a Bytes object in Python

Use the len() function to get the length of a bytes object.

The len() function returns the length (the number of items) of an object and can be passed a sequence (a bytes, string, list, tuple or range) or a collection (a dictionary, set, or frozen set).

Copied!
my_bytes = 'hello'.encode('utf-8') print(type(my_bytes)) # 👉️ print(len(my_bytes)) # 👉️ 5

get length of bytes

The len() function returns the length (the number of items) of an object.

Here is another example with some special characters.

Copied!
my_bytes = 'éé'.encode('utf-8') print(type(my_bytes)) # 👉️ print(len(my_bytes)) # 👉️ 4

The same approach can be used to get the length of a bytearray .

Copied!
my_byte_array = bytearray('hello', encoding='utf-8') print(len(my_byte_array)) # 👉️ 5

If you need to get the size of an object, use the sys.getsizeof() method.

Copied!
import sys my_bytes = 'hello'.encode('utf-8') print(sys.getsizeof(my_bytes)) # 👉️ 38 print(sys.getsizeof('hello')) # 👉️ 54

The sys.getsizeof method returns the size of an object in bytes.

The object can be any type of object and all built-in objects return correct results.

The getsizeof method only accounts for the direct memory consumption of the object, not the memory consumption of objects it refers to.

The getsizeof() method calls the __sizeof__ method of the object, so it doesn't handle custom objects that don't implement it.

# Get the size of a String in Python

If you need to get the size of a string:

  1. Use the len() function to get the number of characters in the string.
  2. Use the sys.getsizeof() method to get the size of the string in memory.
  3. Use the string.encode() method and len() to get the size of the string in bytes.
Copied!
import sys string = 'bobby' # ✅ Get the length of the string (number of characters) print(len(string)) # 👉️ 5 # ------------------------------------------ # ✅ get the memory size of the object in bytes print(sys.getsizeof(string)) # 👉️ 54 # ------------------------------------------ # ✅ get size of string in bytes my_bytes = len(string.encode('utf-8')) print(my_bytes) # 👉️ 5 print(len('őŴœ'.encode('utf-8'))) # 👉️ 6

get size of string

Use the len() function if you need to get the number of characters in a string.

Copied!
print(len('ab')) # 👉️ 2 print(len('abc')) # 👉️ 3

The len() function returns the length (the number of items) of an object.

The argument the function takes may be a sequence (a string, tuple, list, range or bytes) or a collection (a dictionary, set, or frozen set).

Источник

Читайте также:  Insert Image in MySql using PHP
Оцените статью