Home > OS >  OpenRefine: swapping order strings within a column of values
OpenRefine: swapping order strings within a column of values

Time:10-15

I have a column of values with a range of dates formatted as DD Month YYYY, but I want this to read Month DD YYYY. So, for example "14 October 2021" should be "October 14 2021" - is there is a simple way to do this in OpenRefine?

Thank you!

CodePudding user response:

From a google search it looks like there is a python library called Jython. If you install it, you could try.

result = re.sub('(\d ).(\w ).(\d )', r'\2 \1 \3', input) 

14 October 2021

\d  matches 14
\w  matches October 
\d  matches 2021

The () puts them into groups, so (\d ) is the first group and (\w ) is the second group. You then use the \2 \1 \3 to refer to those groups, essentially saying, move group 2, October to the start, group 1, 14, to the middle, and group 3, 2021, stays where it is. This works in R, I don't really use python but it appears you need to use the r' in-front, as above, so the \ treated as an escape character.

CodePudding user response:

You can use the date functions in OpenRefine to parse and format dates.

In your case that would be:

value.toDate("dd MMMM yyyy").toString("MMMM dd yyyy")

Note that using the long form of the months is context sensitive. Meaning that parsing english month names on a french computer won't work.

  • Related