Quantcast
Channel: CNode:Node.js专业中文社区
Viewing all articles
Browse latest Browse all 14821

用 js 遍历数组,然后调用 python 脚本处理数组元素,但得到的结果却是乱序的

$
0
0

1. 代码:

1.1 js脚本

// call_py.js

// 制作数组
var s1="1 What makes Rails so great?"
var s2 = "2 do you love me still ?"
var s3 = "3 The Ruby on Rails Tutorial is designed to give you a thorough introduction to web application developmentntrepreneur."
var s4 = "4 of course i still love you . "
var list=[s1,s2,s3,s4]

// 子进程
var exec = require('child_process').exec;

//	遍历数组
let iterable = list

for(let value of iterable)
{
	exec('python py_test.py '+value+' ',function(error,stdout,stderr)
			{
				if(error){
					console.log(stderr)
				}else{
					console.log(stdout)
				}
			}

		)
}

1.2 python脚本:

# py_test.py
import sys

script = sys.argv
print script

1.3 输出的结果:

['py_test.py', '1', 'What', 'makes', 'Rails', 'so', 'great?']

['py_test.py', '2', 'do', 'you', 'love', 'me', 'still', '?']

['py_test.py', '3', 'The', 'Ruby', 'on', 'Rails', 'Tutorial', 'is', 'designed', 'to', 'give', 'you', 'a', 'thorough', 'introduction', 'to', 'web', 'application', 'developmentntrepreneur.']

['py_test.py', '4', 'of', 'course', 'i', 'still', 'love', 'you', '.']

2.疑问:

2.1 问题一:

  • 为什么输出的结果不是按照数组的顺序来的,而是乱序的?
  • 怎样才能输出按照数组顺序的结果?

2.2 问题二:

  • 因为我在js脚本的第18行用的是'python py_test.py '+value+'',所以我觉得应该是将value作为一个值传递给python脚本,但是实际输出结果却显示是拆分成了很多个词,然后变成了很多个值?

Viewing all articles
Browse latest Browse all 14821

Trending Articles