def convert_count(input): try: import re from datetime import datetime # Function to transform the text def transform_text(CABLE_COUNT): try: import re #create a list to store the transformed values transformed_values = [] #Split input text into a list of strings by ; groups = CABLE_COUNT.split(";") #Loop through each group for group in groups: #Get the parts of the group by splitting on , parts = group.split(",") #remove any whitespace from the parts parts = [part.strip() for part in parts] #print(parts) #If the part contains "CT" remove everything before the last space parts = [part.split(" ")[-1] if "CT" in part else part for part in parts] #If the group ends in "XD", remove the "XD" text if parts[-1].endswith("XD"): parts[-1] = "XD:" + parts[-1][:-2] #If the sequesntial part starts with a P add a , to the end of the current part for i in range(len(parts)): if parts[i].startswith("P"): parts[i-1] += "," #If the current part starts with a P and the next part starts with a T, add a , to the end of the current part otheriwse add a : to the end of the current part for i in range(len(parts)): if parts[i].startswith("P") and parts[i+1].startswith("T") or parts[i].startswith("P") and parts[i+1].isdigit(): parts[i] += "," #If the current part starts with a P and the next part starts with a number and there is not a part after that, add a : to the end of the current part for i in range(len(parts)): if parts[i].startswith("P") and parts[i+1][0:1].isdigit() and i+2 == len(parts): parts[i+1] = "S" + parts[i+1] #parts[i+1] = ":" + parts[i+1] elif parts[i].startswith("P") and parts[i+1][0:1].isdigit(): parts[i] += ":" parts[i] = "S" + parts[i+1] #If the current part starts with a number and the previous part starts with a P add a : to the start of the current part for i in range(len(parts)): if parts[i][0:1].isdigit() and parts[i-1].startswith("P"): parts[i] = "S" + parts[i] #If the current part starts with an S and the next part starts with a T add a : to the end of the current part for i in range(len(parts)): #If this is not the last part if i+1 < len(parts): if parts[i].startswith("S") and parts[i+1].startswith("T"): parts[i] += ":" #If the previous part starts with an S and the current part starts with a number add a T to the beginning of the current part for i in range(len(parts)): if parts[i-1].startswith("S") and parts[i].isdigit(): parts[i] = ":T" + parts[i] #If the current part does not contain a : a , or a ; and is not the last part, add a , to the end of the current part for i in range(len(parts)): if ":" not in parts[i] and "," not in parts[i] and ";" not in parts[i] and i+1 < len(parts): parts[i] += ":" #Combine the parts into a single string combined_parts = "".join(parts) combined_parts = combined_parts.replace(",:", "") #Add the combined parts to the transformed values list transformed_values.append(combined_parts) #Combine the transformed values into a single string with a ; between each value transformed_value = ";".join(transformed_values) #Find semicolons that are after a letter and not before "XD" and replace them with a colon transformed_value = re.sub(r"(?<=[a-zA-Z]);(?![a-zA-Z]*XD)", ":", transformed_value) #Find text where the letter S is followed by any numbers and the numbers are followed by a dash. Keep the first S and the numbers and replace the dash with a dash followed by an S #transformed_value = re.sub(r"(S\d+)-", r"\1-S", transformed_value) #replace ":XD" with ;XD" transformed_value = transformed_value.replace(":XD", ";XD") #Replace ",S" with ":S" transformed_value = transformed_value.replace(",S", ":S") #If there is a trailing ; remove it if transformed_value.endswith(";"): transformed_value = transformed_value[:-1] print(transformed_value) return transformed_value except Exception as e: pass # Get the current time current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") # Get input value from the input box inputCableCountBox = Element("input_box") inputCableCountValue = inputCableCountBox.value # Call the transform_text function result = transform_text(inputCableCountValue) # Update the output box with the result Element("output_box").element.value = result # Record the time, input, and output in the table record_table_body = Element("record_table_body") new_row = record_table_body.insertRow(0) new_row.insertCell(0).textContent = current_time new_row.insertCell(1).textContent = inputCableCountValue new_row.insertCell(2).textContent = result except Exception as e: pass