refine-client-py icon indicating copy to clipboard operation
refine-client-py copied to clipboard

added method for templating export

Open felixlohmeier opened this issue 5 years ago • 1 comments

The OpenRefine Templating supports exporting data in any text format (i.e. to construct JSON or XML). The graphical user interface offers four input fields:

  1. prefix
  2. row template (supports GREL inside two curly brackets, e.g. {{jsonize(cells["name"].value)}})
  3. row separator
  4. suffix

The proposed new method adds the templating feature to the client: export_templating(self, engine='', prefix='', template='', rowSeparator='\n', suffix='')

example

in:

from google.refine import refine

server1 = refine.Refine('http://localhost:3333')

project1 = server1.new_project(project_file='tests/data/duplicates.csv')

data = project1.export_templating(
    prefix='''{ "events" : [
''',
    template='''    { "name" : {{jsonize(cells["name"].value)}}, "purchase" : {{jsonize(cells["purchase"].value)}} }''',
    rowSeparator=''',
''',
    suffix='''
] }''')

print(data.read())

out:

{ "events" : [
    { "name" : "Danny Baron", "purchase" : "TV" },
    { "name" : "Melanie White", "purchase" : "iPhone" },
    { "name" : "D. Baron", "purchase" : "Winter jacket" },
    { "name" : "Ben Tyler", "purchase" : "Flashlight" },
    { "name" : "Arthur Duff", "purchase" : "Dining table" },
    { "name" : "Daniel Baron", "purchase" : "Bike" },
    { "name" : "Jean Griffith", "purchase" : "Power drill" },
    { "name" : "Melanie White", "purchase" : "iPad" },
    { "name" : "Ben Morisson", "purchase" : "Amplifier" },
    { "name" : "Arthur Duff", "purchase" : "Night table" }
] }

felixlohmeier avatar Aug 20 '19 03:08 felixlohmeier

Hi @paulmakepeace, do you still want to receive pull requests? This one is waiting for you :)

felixlohmeier avatar Dec 06 '19 12:12 felixlohmeier