我正在运行以下python代码。我希望它能在终端中执行一些外部Python代码,并将输出保存在一个numpy数组中,然后我可以将该数组附加到另一个numpy数组以添加额外的列。它在shell中运行外部python命令;但我找不到获取输出的方法,以便将其保存在程序中。你知道吗
代码如下:
import csv
import GetAlexRanking #External Method exposed here
import subprocess
import pandas as p
import numpy as np
loadData = lambda f: np.genfromtxt(open(f,'r'), delimiter=' ')
with open('train.tsv','rb') as tsvin, open('PageRanks.csv', 'wb') as csvout:
tsvin = list(np.array(p.read_table('train.tsv'))[:,0])
csvout = csv.writer(csvout)
for row in tsvin:
count = 0
cmd = subprocess.Popen("python GetAlexRanking.py " + row ,shell=True)
(output, err) = cmd.communicate()
exit_code = cmd.wait()
print exit_code #testing
print output #**error here**, always prints "none"
csvout.write(url + "\t" + cmd_string) #writing
count+=1
如何获得“python”的输出GetAlex排名.py命令并将其保存在Python代码中的变量中?你知道吗
谢谢。你知道吗
使用
stdout=subprocess.PIPE
。没有它,子进程就直接将其输出打印到终端。你知道吗相关问题 更多 >
编程相关推荐