How to unpack a string in python

I think the first question to answer is: Why are you packing strings like this in the first place? Unless you are passing this as a data structure to a library that accepts the format you created above, you should not need to do that: store strings as text in text files - not as binary. If it is the case that you need space/performance, use an SQL engine - SQLITE will work fine.

Also attempt for the fact the code above is hard to read Python - it may be the only way to interpolate strings and numbers in Javascript, but in Python, you should use: txt = struct.pack('B%ds' % len(gvrTxt) , len(gvrTxt), gvrTxt) instead of

txt = struct.pack('B' + str(len(gvrTxt)) + 's', len(gvrTxt), gvrTxt)

You can't unpack this with the struct.unpack method without manually slicing it first, as you've noted, since UNPACK requires that the passed byte-string contained the packed values to be equal in size to the format passed.

You can retrieve the size by slicing first the size of the first field, and passing it to unpack, and then, unpack the remaining of the struct:

length = struct.unpack("B", text[0:1])[0]
gvrTxt = struct.unpack("%ds" % length, text[1:][0]

But of course you could not use struct at all, if you are using just bytestrings:

gvrTxt = text[1:]

If you concatenate the above structures together, doing something like:

data = dataType + varName + txt

you have to unpack then separately, using the length of varName to know where to pick the start of txt

datatype = struct.unpack("H", data[0:2])
lenvarname = ord (data[2])
varName = data[3: 3 + lenvarname]
txt = data [ 4+ lenvarname:]

But I insist, this is usually not needed in a Python program - you only will need this if you are generating a very specific file for another application to consume, or calling a Library in native code which does have rather improper Python bindings.

Also, pay attention that these techniques do not allow for the proper handling of "text" since there is no warranty that any text content will have one byte per character: please read http://www.joelonsoftware.com/articles/Unicode.html before coding any further, even if you think you will never find a unicode character in your life. Because you will. And your program will hit them first.

Summary: in this tutorial, you’ll learn how to unpack a list in Python to make your code more concise.

Introduction to the list unpacking

The following example defines a list of strings:

colors = ['red', 'blue', 'green']

Code language: Python (python)

To assign the first, second, and third elements of the list to variables, you may assign individual elements to variables like this:

red = colors[0] blue = colors[1] green = colors[2]

Code language: Python (python)

However, Python provides a better way to do this. It’s called sequence unpacking.

Basically, you can assign elements of a list (and also a tuple) to multiple variables. For example:

red, blue, green = colors

Code language: Python (python)

This statement assigns the first, second, and third elements of the colors list to the red, blue, and green variables.

In this example, the number of variables on the left side is the same as the number of elements in the list on the right side.

If you use a fewer number of variables on the left side, you’ll get an error. For example:

colors = ['red', 'blue', 'green'] red, blue = colors

Code language: Python (python)

Error:

ValueError: too many values to unpack (expected 2)

Code language: Python (python)

In this case, Python could not unpack three elements to two variables.

Unpacking and packing

If you want to unpack the first few elements of a list and don’t care about the other elements, you can:

  • First, unpack the needed elements to variables.
  • Second, pack the leftover elements into a new list and assign it to another variable.

By putting the asterisk (*) in front of a variable name, you’ll pack the leftover elements into a list and assign them to a variable. For example:

colors = ['red', 'blue', 'green'] red, blue, *other = colors print(red) print(blue) print(other)

Code language: Python (python)

Output:

red blue ['green']

Code language: Python (python)

This example assigns the first and second elements of the colors list to the red and green variables. And it assigns the last element of the list to the other variable.

Here’s another example:

colors = ['cyan', 'magenta', 'yellow', 'black'] cyan, magenta, *other = colors print(cyan) print(magenta) print(other)

Code language: Python (python)

Output:

cyan magenta ['yellow', 'black']

Code language: Python (python)

This example assigns the first and second elements to variables. It packs the last two elements in a new list and assigns the new list to the other variable.

Summary

  • Unpacking assigns elements of the list to multiple variables.
  • Use the asterisk (*) in front of a variable like this *variable_name to pack the leftover elements of a list into another list.

Did you find this tutorial helpful ?

How do I unpack an element in Python?

Summary. Unpacking assigns elements of the list to multiple variables. Use the asterisk (*) in front of a variable like this *variable_name to pack the leftover elements of a list into another list.

What is unpack Python?

Unpacking in Python refers to an operation that consists of assigning an iterable of values to a tuple (or list ) of variables in a single assignment statement. As a complement, the term packing can be used when we collect several values in a single variable using the iterable unpacking operator, * .

How do you split a string variable in Python?

To split string variables at each whitespace, we can use the split() function with no arguments. The syntax for split() function is split(separator, maxsplit) where the separator specifies the character at which the string should be split. maxsplit specifies the number of times the string has to be split.

How do I unpack an iterable in Python?

There are 2 ways to unpack iterables in Python. For known length iterables - Providing the exact number of variables to unpack as the number of elements in the sequence/iterable. For arbitrary length iterables - Using star expressions (*) to unpack when you are unsure of the number of variables to pass.