Python script, can not process list


I’m having trouble running a Python script for a list.
The script works perfectly for a single string.
I searched for a solution on this forum but couldn’t solve the problem yet, probably because it’s my first Python code and I don’t have any basics in programming :slightly_smiling_face:.

The script converts a binary code into text.

Enable Python support and load DesignScript library

import clr
from Autodesk.DesignScript.Geometry import *

The inputs to this node will be stored as a list in the IN variables.

a_binary_string = IN[0]

#Split into blocks of 8
n = 8
binary_values = [a_binary_string[index : index + n] for index in range(0, len(a_binary_string), n)]

#Convert to base 2 decimal integer
ascii_string = “”
for binary_value in binary_values:
an_integer = int(binary_value, 2)

#Convert to ASCII character
ascii_character = chr(an_integer)

#Append character to ascii_string
ascii_string += ascii_character

Assign your output to the OUT variable.

OUT = ascii_string

I think the problem is in splitting the input in a sublist of 8 character blocks.

Could someone tell me how to run this code for each instance of the list, or even better, give me the working script and explain it to me?


You have to create a loop over binary strings. Now I don’t have time to solve it but you can try to do it what I mean.

What is the error message?

Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed.
Traceback (most recent call last):
File “”, line 16, in
TypeError: expected str, got list

Line 16 is;

an_integer = int(binary_value, 2)

So I can understand now that it works for a single string, but not for a list.
But I have no idea how to solve it.

I searched the internet how to loop this script.
Maybe the solution is into making this line accept lists?

Hi @DynH

Welcome to Dynamo Forum!

Here is the solution that would handle for you any list structure:

#Function to handle any list structure
ProcessLists = lambda function, lists: [ProcessLists(function, item) if isinstance(item, list) else function(item) for item in lists]
ApplyFunction = lambda func, objs: ProcessLists(func, objs) if isinstance(objs, list) else [func(objs)]

#Function to convert binary to text
def task_convert_bin_totext(data):
    for letter in data.split():
        result+=chr(int(letter, 2))
    return result

OUT = ApplyFunction(task_convert_bin_totext,IN[0])


Thank you for the help.
This works perfectly.