0

Working on my final project and I'm stuck trying to edit user input. The below code should take the first string in a list and check if it's numeric. If it is, great, that's what I want, otherwise, I need to find out where the non-numeric characters are and insert a space between the numbers and the letters, e.g. '110g' -> '110' 'g'

if not item[0].isnumeric():
    count = 0
    for c in item[0]:
        if c == r'[0-9\.]':
            print(c)
            count += 1
            continue
        else:
            measure = insert_str(item[0], " ", count)
            print(measure)
            measureList = []
            measureList = measure.split(" ")
            print(measureList)
            item[0] = measureList[0]
            item.insert(1, measureList[1])
            break

I've run the debugger a few times and it seems that my second if statement if c == r'[0-9\.]': isn't catching anything, but I can't figure out why. It could be that my regex is completely wrong but from what I've read, I think I'm doing it right. Any ideas anyone?

0

I've managed to fix my issue by switching from using RegEx to using

if c.isdigit():
    ...
elif: c == '.':
    ...
else:
    ...

I'd appreciate knowing where I went wrong with the RegEx though and how I can improve it as it is definitely not DRY code.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .