我正在尝试编写一个Python函数,它使用gdal将给定的坐标系转换为另一个坐标系。问题是我试图将命令作为一个字符串执行,但是在shell中,在输入坐标之前必须按enter键。在
x = 1815421
y = 557301
ret = []
tmp = commands.getoutput( 'gdaltransform -s_srs \'+proj=lcc +lat_1=34.03333333333333
+lat_2=35.46666666666667 +lat_0=33.5 +lon_0=-118 +x_0=2000000 +y_0=500000 +ellps=GRS80
+units=m +no_defs\' -t_srs epsg:4326 \n' + str(x) + ' ' + str(y) )
我用'\n'试过了,但没用。在
我的猜测是,通过按Enter键运行
gdaltransform
,坐标由程序本身从其stdin读取,而不是从shell读取:如果我不需要在命令和命令之间执行错误的话。在
更准确地说,使用
^{pr2}$shell=True
(我刚才所说的)是,如果给定一个命令字符串而不是一个列表,那么就应该使用它。如果您想使用列表,建议如下:然后捕获输出,或者您可以使用开放流并使用
handle.stdin.write()
,但这有点棘手。在除非您只想执行、读和死,
.communicate()
是完美的,或者只是.check_output(<cmd>)
关于
Popen
如何工作的好信息可以在这里找到(尽管主题不同):python subprocess stdin.write a string error 22 invalid argument解决方案
无论如何,这个应该起作用(您必须重定向STDIN和STDOUT):
相关问题 更多 >
编程相关推荐