Running a python script from nodejs and returning json back

Refresh

April 2019

Views

18 time

1

I am running a python script using python-shell. The python script(named combine.py) returns data in json format. But the code works fine on my local machine but not on aws instance. I get the following error in pm2 logs:

SyntaxError: Unexpected token / in JSON at position 0

I have made sure that python script returns valid json data by first putting data in file and then checking that data via online json validator.

Nodejs (javascript file)

  var ps = require('python-shell')
  noOfLines = 2
  noOfClusters = 5
  var options = {
    mode: 'json',
    pythonOptions: ['-u'], // get print results in real-time
    scriptPath: './pythonScripts/',
    args: [noOfClusters, noOfLines, processingData]
  }

  ps.PythonShell.run('combine2.py', options, function(err, results) {
    if (err) throw err
    // Results is an array consisting of messages collected during executio n
    //console.log(results)

    // Data send to index_timeline
    res.render('index_timeline', {
      results: results[0]
    })

    fs.writeFile('myOutput.txt', JSON.stringify(results, 0, 2), err => {
      // throws an error, you could also catch it here
      if (err) throw err

      // success case, the file was saved
      console.log('File saved!')
    })
  })

Python Script(combine.py)

sentences = dataCleaning(content).split('.') 
sentences = list(filter(None, sentences))

temp=list()
myDict=dict()
summarizing=str()

#getting clusters
clusters = cluster_sentences(sentences, nclusters)


for cluster in range(nclusters):
    for i,sentence in enumerate(clusters[cluster]):
        temp.append(sentences[sentence])
        summarizing+=sentences[sentence]+". "
    myDict["sentences"]=list(temp)
    sentence_tokens, word_tokens = tokenize_content(summarizing)
    sentence_ranks = score_tokens(word_tokens, sentence_tokens)
    myDict["summary"]=str(summarize(sentence_ranks, sentence_tokens,noOfLines))
    data["clusters"].append(dict(myDict))
    myDict.clear()
    del temp[:]
    summarizing=''

print(json.dumps(data))

myOutput.txt(data that I wrote in txt file above)

[
  {
    "clusters": [
      {
        "sentences": [
          " Qoum & Maa Baap K Duaon Se Award Haasil Kiya",
          " Qoum & Maa Baap K Duaon Se Award Haasil Kiya",
          " Qoum & Maa Baap K Duaon Se Award Haasil Kiya"
        ],
        "summary": " Qoum & Maa Baap K Duaon Se Award Haasil Kiya. Qoum & Maa Baap K Duaon Se Award Haasil Kiya."
      },
      {
        "sentences": [
          " Mushtaq Ahmed",
          " Mushtaq Ahmed",
          " Mushtaq Ahmed NIJAMHAMY"
        ],
        "summary": " Mushtaq Ahmed. Mushtaq Ahmed NIJAMHAMY."
      }
    ]
  }
]

0 answers