Search code examples
pythonjythonopenrefine

Openrefine, get all the numbers from an interval


I've got cells like:

031 - 039

and I want to trasform it into different cells with all the numbers inbetween:

031 032 033 034 035 036 037 038 039

How can I do it?

Update

Trying to work with python/jython:

if " - " in value:
   number_list = []
   first = int(value.split(" - ")[0])
   last = int(value.split(" - ")[1])
   for i in range(first, last):
       i += 1
       num = str(i)
       number_list.append(num)
   return str(first)+"|"+"|".join(number_list)
else:
   return value

I don't really now how to create a new cell in jython with the desired values. So I will do that in a second step, with a "normal" split by "|".


Solution

  • You are on the right track with your update, although you can simplify it quite a bit.

    >>> first,last = "31 - 39".split(" - ")
    >>> "|".join([format(i,"03") for i in range(int(first),int(last)+1)])
    '031|032|033|034|035|036|037|038|039'
    

    There's no way to create new rows/cells, so using "Split multi-valued cells" is your best bet.