【发布时间】:2017-04-14 22:07:03
【问题描述】:
我正在编写一个 python 程序,它必须运行一个 c++ 程序(已编译)才能管理非常重的负载操作。
我调用这个可执行文件的方式是 subprocess.check_output()
这样做,它会返回一个很长的字符串,如下所示:
executable_output_1 = [0.011, 0.544, 2.314], [7.895, 6.477, 2.573]
executable_output_2 = [4.255, 6.235, 7.566], [9.522, 7.321, 1.234]
type(executable_output) >>> <type 'str'>
在这个例子中我写了一个很短的字符串,但在实际输出中它真的很长。
我想在 python 中对这些数据进行一些操作,所以我需要一个列表列表(因为我会多次调用该可执行文件)。
如何将该字符串转换为列表列表?
期望的输出:
executables_outputs_list = [[[0.011, 0.544, 2.314], [7.895, 6.477, 2.573]], [[4.255, 6.235, 7.566], [9.522, 7.321, 1.234]]]
type(executable_output_list) >>> <type 'list'>
type(executable_output_list[0]) >>> <type 'list'>
type(executable_output_list[0][0][0]) >>> <type 'float'>
【问题讨论】:
-
我想查看带有 subprocess.check_output() 调用的代码。
-
@Back2Basics 很简单:
check_output(['/home/user/executable', '/home/user/image.png']) -
好吧,您可能会在
=上拆分并将字符串包装在[和]中,然后使用ast.literal_eval。丑陋,而且肯定是 hackey。 -
@Back2Basics
executable_output_1 = check_output(['/home/user/executable', '/home/user/image.png'])不仅仅是图片,还有一些从中处理的数据。
标签: python list type-conversion