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 .
The script converts a binary code into text.
Enable Python support and load DesignScript library
import clr
clr.AddReference(‘ProtoGeometry’)
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
#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):
result=""
for letter in data.split():
result+=chr(int(letter, 2))
return result
#Output
OUT = ApplyFunction(task_convert_bin_totext,IN[0])