If I have a string like ABC1.23 or XYZ4.56a, I just want the ABC and XYZ (I do not want XYZa).
Basically, it’s sheet numbers and I want all the letters at the beginning of the sheet number before the first numerical character. So, A1.4 becomes A, TE3.2a, becomes just TE, etc.
This helps me determine the discipline of the sheets for further sorting later (we use a shared param amongst ourselves and consultants for sheet list sorting).
I found this thread, but it’s just removing all numbers from the string, so I would end up with ‘TE.a’ from ‘TE3.2a’ using that method.
I know one of you dynamo/python gurus do this stuff in your sleep.
I’ll keep searching in the meantime.
I think I got it, found another solution on that same thread and reworked for my purposes.
The graph below will return all letters up to the first numerical character in a string.
I realize you have a solution, but this is just as a reminder:
Why? If you don’t tell us the reason, we can’t tell you how.
Is it always the first 3 characters? Just pull the substring.
Is it uppercase only? Check for uppercase letters.
Is it any set of 3 uppercase letters? Use regex.
Is it based on the location of the “.”? Split the string.
Based on your solution, it seems to be the last option and you figured out a solution using that same method. Other people looking for answers may not realize that unless you explain the full situation of what you’re looking for and why.
@Nick_Boyts
Sorry. I thought it was pretty straightforward given my examples. I can see how other scenarios can fit though.
Basically, it’s sheet numbers and I want all the letters at the beginning of the sheet number before the first numerical character. So, A1.4 becomes A, TE3.2a, becomes just TE, etc.
This helps me determine the discipline of the sheets for further sorting later (we use a shared param amongst ourselves and consultants for sheet list sorting).
I have no idea how many characters it will be, therefore I can’t use substring (that was my initial solution until we ended up getting sheets that had two letters I needed to account for).
Almost always uppercase, but I also can’t know that.
Has nothing to do with the ‘.’ since there’s a number before the ‘.’ that is also removed.
What I was looking to do is find the first numerical character and split on that.
I see now that I inadvertently linked directly to your answer on the other thread and not just to the thread. Made it seem like I was making a direct comparison to that. Apologies.
All good. My comment was just to point out that there are many ways to solve a problem, and without adequate constraints, the number of potential solutions becomes more harmful than helpful. It also forces us as developers and problem solvers to really understand the problem we’re trying to solve. It’s not always the one we think we’re solving.
I even misread your solution and still assumed it was a different problem you were solving. Just goes to show you how detailed we really need to be in most cases. It sounds like you have the right solution for your problem though. Finding the first number in the string and returning the leasing substring gets you what you want, assuming that the string always starts with a letter.
Agreed, there are still assumptions being made based on how we mandate sheet numbering structure. But I have to make some assumptions or the script will be monumental!
I completely agree I should be as detailed as possible. I’m one of the people hunting through these forums for solutions and knowing if the scenario matches mine or is close enough to glean some technique is very important!
You’re always a name I look for on here for great ideas! Thanks for all the solutions!
aa=IN[0]
def firstnum(a):
if True in [x.isdigit() for x in a]:
return a[:[x.isdigit() for x in a].index(True)]
else:
return a
OUT= [firstnum(a) for a in aa]
str.isdigit() will allow punctuation through and the list comprehensions and True test are running through the entire string. You could enumerate the string, test for a character and if that returns False return the slice. This will only return the first letters of a string, otherwise an empty string
def sheet_prefix(sheet_number):
for i, c in enumerate(sheet_number):
if not c.isalpha():
return sheet_number[:i]
hi,
Rather than testing the entire chain (as I did), we scan and stop as soon as the Boolean is no longer correct
It’s faster than scanning everything (this is the principle of enumerate if I understood correctly)
thank you @you
Sincerely
christian.stan
I don’t know all the details of the fundamental changes to Revit 2025. Is Python still fine to use going forward? Is it just IronPython2 that is deprecated in Revit?
IronPython2 has not been supported since January of 2022 if I recall correctly, so no version of Revit 2024 or up has had a secure version of IronPython2 in it. The CPython engine is formally supported though, and the IronPython3 engine is available as well with informal support.