【问题标题】:Difficulty piping with qhull through python通过python使用qhull管道困难
【发布时间】:2013-09-05 02:23:10
【问题描述】:

我在 python 中通过 QHull 管道命令时遇到问题。我目前正在尝试这样做:

input_command = "rbox c " + str(qpoints) + " | qconvex FQ FV n"
command = subprocess.Popen(input_command.split(" "), stdout=subprocess.PIPE)
print command.communicate()[0]

在这里,qpoints 被格式化,因此 input_command 最终为:

rbox c P0,0,0 P0,0,2 P0,2,0 P0,2,2 P2,0,0 P2,0,2 P2,2,0 P2,2,2 | qconvex FQ FV n

不幸的是,这只是打印出 qconvex 的用法:

qconvex- compute the convex hull.  Qhull 2012.1 2012/02/18
    input (stdin): dimension, number of points, point coordinates
    comments start with a non-numeric character

options (qconvex.htm):
    Qt   - triangulated output
    QJ   - joggled input instead of merged facets
    Tv   - verify result: structure, convexity, and point inclusion
    .    - concise list of all options
    -    - one-line description of all options

output options (subset):
    s    - summary of results (default)
    i    - vertices incident to each facet
    n    - normals with offsets
    p    - vertex coordinates (includes coplanar points if 'Qc')
    Fx   - extreme points (convex hull vertices)
    FA   - report total area and volume
    FS   - compute total area and volume
    o    - OFF format (dim, n, points, facets)
    G    - Geomview output (2-d, 3-d, and 4-d)
    m    - Mathematica output (2-d and 3-d)
    QVn  - print facets that include point n, -n if not
    TO file- output results to file, may be enclosed in single quotes

examples:
    rbox c D2 | qconvex s n                    rbox c D2 | qconvex i
    rbox c D2 | qconvex o                      rbox 1000 s | qconvex s Tv FA
    rbox c d D2 | qconvex s Qc Fx              rbox y 1000 W0 | qconvex s n
    rbox y 1000 W0 | qconvex s QJ              rbox d G1 D12 | qconvex QR0 FA Pp
    rbox c D7 | qconvex FA TF1000

我在网上阅读了一些在 python 调用中包含管道时必须采取的额外步骤的示例。但是我无法让它们的任何示例起作用,而且几乎没有关于发生了什么的解释。有人可以在这里向我解释一个有效的代码 sn-p 以及为什么有效吗?

我也尝试从文件中读取一个函数的结果。例如,我尝试从文件中读取 rbox 的结果:

python 代码:

input_command =  "qconvex FQ FV n < rbox.txt"
    command = subprocess.Popen(input_command.split(" "), shell=True)
    result = command.communicate()
    return result

数据:

3 rbox c P1,1,1 P1,1,3 P1,3,1 P1,3,3 P3,1,1 P3,1,3 P3,3,1 P3,3,3
16
     1      1      1 
     1      1      3 
     1      3      1 
     1      3      3 
     3      1      1 
     3      1      3 
     3      3      1 
     3      3      3 
  -0.5   -0.5   -0.5 
  -0.5   -0.5    0.5 
  -0.5    0.5   -0.5 
  -0.5    0.5    0.5 
   0.5   -0.5   -0.5 
   0.5   -0.5    0.5 
   0.5    0.5   -0.5 
   0.5    0.5    0.5 

这仍然只是打印出 QConvex 描述。奇怪的是,这在命令行中工作得很好,而不是通过 python。即使我不能让管道工作,我也绝对需要从文件中读取才能工作。有谁知道这个函数调用的诀窍是什么?

【问题讨论】:

    标签: python linux subprocess pipe qhull


    【解决方案1】:
    • 如果您使用|等shell功能或使用纯Python重写命令,请使用shell=True,请参阅Replacing shell pipeline
    • 如果你使用shell=True 那么将命令作为the docs中指定的字符串传递
    from subprocess import check_output as qx
    
    output = qx("rbox c {qpoints} | qconvex FQ FV n".format(qpoints=qpoints),
                shell=True)
    print output
    

    【讨论】:

    • 非常感谢您的回复;立即检查!
    猜你喜欢
    • 1970-01-01
    • 2021-09-13
    • 2021-03-30
    • 1970-01-01
    • 2011-12-11
    • 1970-01-01
    • 2018-10-05
    • 1970-01-01
    • 2011-03-17
    相关资源
    最近更新 更多