Number/String Formatting

I have a list of number and strings all formatted differently, see the list below.

I need to have them all export in the same format, 12 34 56, or 12 34 56.78

Existing = Desired
78934 = 07 89 34
07 21 34 = 07 31 34
09 33 43.13 = 09 33 43.13
094309 = 09 43 09
104565.21 = 10 45 65.21

I have a graph which does this (poorly) with more than 20 nodes. Wondering if there is an easier way to get the desired output. I am happy to share the graph but am really looking for new eyes/thoughts.

My current methodology is to convert everything to strings and then manipulate them as strings rather than numbers to get the “12 34 56” format

It’s always helpful to show what you have so far. You may just be missing one little thing to improve the efficiency of your graph or you may be missing something fundamental.

It would also be helpful to know where these values are coming from? Why are they in different formats to begin with?

1 Like

I have attached my current graph for reference.
The values are coming from an excel document. The number format is the US construction specification format. When reading the excel values, sometimes they come in as strings and sometimes they come in as floats. Currently, I convert everything to strings, split them at the decimal (if there is one), add a leading “0” if it is only a 5 digit value, convert it to the 12 34 56 format, then add the decimal portion if there is one, i.e. 12 34 56.78. This then gets pushed into Revit to populate our material schedules.
formatting.dyn (261.7 KB)

An image is much easier to see and doesn’t require us to rerun your graph (which would require your Excel file and any packages you’re using). Do the values follow the correct format in Excel? Do the cells use a specified format there? If your data (formatting) is bad that’s one thing, but if the format is lost during import that’s something else.

Yes, the formatting is lost upon import.
This is the excel import

These show the main formatting

You need to show the node previews otherwise we have no idea what your data looks like or what issues you may be running into.

Then you may be able to just fix the import rather than redoing all the work. Are the cells in Excel string formatted? Is your Excel node set to read the data as strings?

This is only possible if your numbers with a decimal are always 2 decimal places, otherwise if they aren’t trailing it would be easily misinterpret-able. Likewise all digits would need to be 2 at most (e.g. 100+ would not work). You would also need to ensure numbers do not truncate to one digit (e.g. 7 should be 07). It’s impossible to know which of the three numbers is 1 or 2 digits otherwise.

You would need to walk through the strings 2 characters at a time and check for presence of a decimal each time, if so grab the next 2 characters in addition and keep walking afterwards. If you encounter a decimal or space 2 digits beyond the current one, process the extra characters and walk a bit further. Once you’ve walked three times you should have the 3 numbers and can join then with a space using the " ".join(strings) method, or using String.Join node if not using Python.

# Example strings
strings = ["123456", "12.345678", "1234.5678", "12 3456", "12 34 56"]

# Strings out
strings_out = []

# For each string
for s in strings:
    # Set base variables
    step = 0
    ind   = 0
    strList = []
    # While we've taken less than 3 steps
    while step < 3:
        # If this is step 3, just give the remainder
        if step == 2:
            strList.append(s[ind:])
        # If character 2 after current is a dot...
        elif s[ind+2] == ".":
            # Apennd current index + 5 (XX.XX)
            strList.append(s[ind:ind+5])
            # Move 5 spots forward
            ind += 5
        # If character 2 after current is a space
        elif s[ind+2] == " ":
            # Append current + 2 (XX)
            strList.append(s[ind:ind+2])
            # Move 3 spots forward
            ind += 3
        # Otherwise, whole number
        else:
            # Append next 2 numbers
            strList.append(s[ind:ind+2])
            # Move 2 spots forward
            ind += 2
        # On to next step
        step += 1
    # Join the strings with a space
    str_out = " ".join(strList)
    # Append to list
    strings_out.append(str_out)

# Results
print(strings_out)

This is great, thanks @GavinCrump !!

1 Like