我正在从node.js调用一个python脚本作为子进程,python脚本从我上传到我的应用程序的文件中提取数据,当我通过应用程序上传文件时,我得到以下错误“UnhandledPromisejectionWarning:SyntaxError:JSON中的意外标记D位于位置1,但当我在python脚本中声明的数据样本上测试python脚本时,它工作得很好,我该如何解决这个问题
以下是我使用的代码:
import sys
import re
import json
from pathlib import Path
from collections import defaultdict
file_path = (Path(__file__).parent / "../config/kal_ejournal.json").absolute()
print(file_path)
with open(file_path) as jsonFile:
jsonObject = json.load(jsonFile)
rCARD_NUMBER = jsonObject['Dictionary']['rCARD_NUMBER']['regex']
bCARD_NUMBER = jsonObject['Field_extraction_rules']['bCARD_NUMBER']
regex = rCARD_NUMBER*bCARD_NUMBER
# re.DOTALL to match any characters including newline
input = open(sys.argv[1], "r")
# print(input.read())
matches = re.findall(regex, input.read(), re.DOTALL)
print(json.dumps(matches))
下面是Node.js中的代码
const python = spawn("python", [
"./data/DataExtractor_2.py",
req.file.path,
]);
// collect data from script
python.stdout.on("data", function (data) {
console.log("Pipe data from python script ...");
largeDataSet.push(data);
});
// in close event we are sure that stream is from child process is closed
python.on("close", async (code) => {
console.log(`child process close all stdio with code ${code}`);
const pythonRes = largeDataSet.join("");
var json = JSON.parse("[" + pythonRes + "]");
var json = [].concat.apply([], json);
const tocsv = ConvertToCSV(json);
let tojson = await csv().fromString(tocsv); // convert csv to json
}
目前没有回答
相关问题 更多 >
编程相关推荐