如何在OpenRefine ReST-API的"Create Project" Post Rquest中传递选项JSON?
How to pass the options JSON in the "Create Project" Post Rquest of the OpenRefine ReST-API?
我目前正在尝试将 Excel 表(作为 .xls)上传到 Ontotexts GraphDB 的 OpenRefine(或 OntoRefine)模块。由于我在上传 xls 时遇到问题,我决定先将 xls 文件转换为 csv 文件,然后再上传。不幸的是,OpenRefine 不会每次都自动将文件识别为 CSV。因此,每行中的所有数据都存储在单个列中。例如:
--------------------------------------------------
| Col1, Col2, Col3, Col4 |
--------------------------------------------------
| Row11, Row12, Row13, Row14 |
--------------------------------------------------
| Row21, Row22, Row23, Row24 |
--------------------------------------------------
而不是:
--------------------------------------------------
| Col1 | Col2 | Col3 | Col4 |
--------------------------------------------------
| Row11 | Row12 | Row13 | Row14 |
--------------------------------------------------
| Row21 | Row22 | Row23 | Row24 |
--------------------------------------------------
随着 Post 请求
POST /command/core/create-project-from-upload
'format' 参数中的文件格式和 'options' 参数中带分隔符的 json 可以添加到 POST 请求中。但是,这也不起作用,官方 OpenRefine 文档 (https://github.com/OpenRefine/OpenRefine/wiki/OpenRefine-API) 不包含任何关于 'options' JSON.
语法的提示
我当前的代码如下所示:
import os
import xlrd
import csv
import requests
import re
xls_file_name_ext = os.path.basename('excel_file.xls')
# create the filename with path to the new csv file (path + name stays the same)
csv_file_path = os.path.dirname(xls_file_name_ext) + '/' + os.path.splitext(xls_file_name_ext)[0] + '.csv'
# remove all comma in xls file
xls_wb = xlrd.open_workbook(xls_file_name_ext)
xls_sheet = xls_wb.sheet_by_index(0)
for col in range(xls_sheet.ncols):
for row in range(xls_sheet.nrows):
_new_cell_val = str(xls_sheet.cell(row, col).value).replace(",", " ")
xls_sheet._cell_values[row][col] = _new_cell_val
# write to csv
with open(csv_file_path, 'w', newline='', encoding='utf-8') as csv_file:
c_w = csv.writer(csv_file, delimiter=',')
for row in range(xls_sheet.nrows):
c_w.writerow(xls_sheet.row_values(row))
ontorefine_server = 'http://localhost:7200/orefine'
# filename of csv as project name in OntoRefine
onterefine_project_name = os.path.splitext(os.path.basename(csv_file_path))[0]
# the required paraneters for the post request
ontorefine_data = {"project-name": onterefine_project_name,
"format": "text/line-based/*sv",
"options": {
"separator": ","
}
}
ontorefine_file = {'project-file': open(csv_file_path, "rb")}
# execute the post request
ontorefine_response = requests.post(
ontorefine_server + '/command/core/create-project-from-upload', data=ontorefine_data, files=ontorefine_file
)
我假设我传递的 POST 请求参数不正确。
当然,这完全取决于您的输入数据,但格式看起来还不错。如果您尝试从 UI 导入,这是 OntoRefine 所做的 "behind the curtains"。您可以通过拦截您的网络流量来查看相同的负载:
{
"format": "text/line-based/*sv",
"options": {
"project-name":"Your-project-here",
"separator":","
}
由此看来,项目名称位置似乎是唯一的区别。这是一个执行相同操作的 curl 命令:
curl 'http://localhost:7200/orefine/command/core/importing-controller?controller=core%2Fdefault-importing-controller&jobID=1&subCommand=create-project' -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' --data 'format%3Dtext%2Fline-based%2F*sv%26options%3D%7B%22separator%22%3A%22%2C%22%22projectName%22%3A%22Your-project-name%22%7D'
我目前正在尝试将 Excel 表(作为 .xls)上传到 Ontotexts GraphDB 的 OpenRefine(或 OntoRefine)模块。由于我在上传 xls 时遇到问题,我决定先将 xls 文件转换为 csv 文件,然后再上传。不幸的是,OpenRefine 不会每次都自动将文件识别为 CSV。因此,每行中的所有数据都存储在单个列中。例如:
--------------------------------------------------
| Col1, Col2, Col3, Col4 |
--------------------------------------------------
| Row11, Row12, Row13, Row14 |
--------------------------------------------------
| Row21, Row22, Row23, Row24 |
--------------------------------------------------
而不是:
--------------------------------------------------
| Col1 | Col2 | Col3 | Col4 |
--------------------------------------------------
| Row11 | Row12 | Row13 | Row14 |
--------------------------------------------------
| Row21 | Row22 | Row23 | Row24 |
--------------------------------------------------
随着 Post 请求
POST /command/core/create-project-from-upload
'format' 参数中的文件格式和 'options' 参数中带分隔符的 json 可以添加到 POST 请求中。但是,这也不起作用,官方 OpenRefine 文档 (https://github.com/OpenRefine/OpenRefine/wiki/OpenRefine-API) 不包含任何关于 'options' JSON.
语法的提示我当前的代码如下所示:
import os
import xlrd
import csv
import requests
import re
xls_file_name_ext = os.path.basename('excel_file.xls')
# create the filename with path to the new csv file (path + name stays the same)
csv_file_path = os.path.dirname(xls_file_name_ext) + '/' + os.path.splitext(xls_file_name_ext)[0] + '.csv'
# remove all comma in xls file
xls_wb = xlrd.open_workbook(xls_file_name_ext)
xls_sheet = xls_wb.sheet_by_index(0)
for col in range(xls_sheet.ncols):
for row in range(xls_sheet.nrows):
_new_cell_val = str(xls_sheet.cell(row, col).value).replace(",", " ")
xls_sheet._cell_values[row][col] = _new_cell_val
# write to csv
with open(csv_file_path, 'w', newline='', encoding='utf-8') as csv_file:
c_w = csv.writer(csv_file, delimiter=',')
for row in range(xls_sheet.nrows):
c_w.writerow(xls_sheet.row_values(row))
ontorefine_server = 'http://localhost:7200/orefine'
# filename of csv as project name in OntoRefine
onterefine_project_name = os.path.splitext(os.path.basename(csv_file_path))[0]
# the required paraneters for the post request
ontorefine_data = {"project-name": onterefine_project_name,
"format": "text/line-based/*sv",
"options": {
"separator": ","
}
}
ontorefine_file = {'project-file': open(csv_file_path, "rb")}
# execute the post request
ontorefine_response = requests.post(
ontorefine_server + '/command/core/create-project-from-upload', data=ontorefine_data, files=ontorefine_file
)
我假设我传递的 POST 请求参数不正确。
当然,这完全取决于您的输入数据,但格式看起来还不错。如果您尝试从 UI 导入,这是 OntoRefine 所做的 "behind the curtains"。您可以通过拦截您的网络流量来查看相同的负载:
{
"format": "text/line-based/*sv",
"options": {
"project-name":"Your-project-here",
"separator":","
}
由此看来,项目名称位置似乎是唯一的区别。这是一个执行相同操作的 curl 命令:
curl 'http://localhost:7200/orefine/command/core/importing-controller?controller=core%2Fdefault-importing-controller&jobID=1&subCommand=create-project' -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' --data 'format%3Dtext%2Fline-based%2F*sv%26options%3D%7B%22separator%22%3A%22%2C%22%22projectName%22%3A%22Your-project-name%22%7D'