Skip to article frontmatterSkip to article content

A lot of people don’t know this, but OpenRefine hides a secret: Python comes already built-in! It turns out that anywhere you can use a GREL expression you can use Python instead by picking it from the “Language” drop-down list:

Screenshot of OpenRefine showing an expression dialog with “Python/Jython” selected as the language

Screenshot of OpenRefine showing an expression dialog with “Python/Jython” selected as the language

Our first Python script

Let’s try it now!

When we first open the “Transform...” option for a column, the default GREL expression is simply:

value

As a reminder: value refers to the current value of each cell in the column, so this expression produces that same value unchanged, which we can see in the preview. When using Python, value still refers to the current value of each cell. However, if we use this on its own in a Python expression it doesn’t quite work.

When writing Python in OpenRefine, we need to be more explicit about what the result of our computation is. The way we do this is to write return <our result> on a separate line:

return value