将字典数组传递给python。

时间:2021-09-26 23:06:52

I have a bash script that builds a dictionary kind of structure for multiple iterations as shown below:

我有一个bash脚本,它为多个迭代构建了一个字典类型的结构,如下所示:

{ "a":"b", "c":"d", "e":"f"}
{ "a1":"b1", "c1":"d1", "e1":"f1", "g1":"h1" }

I have appended all of them to an array in shell script and they are to be fed as an input to python script and I want the above data to be parsed as list of dictionaries.

我已经将它们添加到shell脚本中的一个数组中,它们将作为python脚本的输入进行输入,我希望将上面的数据作为字典的列表进行解析。

I tried some thing like this and it didn't work.

我试了一些这样的东西,但没有用。

var=({ "a":"b", "c":"d", "e":"f"} { "a1":"b1", "c1":"d1", "e1":"f1", "g1":"h1" })

function plot_graph {
RESULT="$1"   python - <<END
from __future__ import print_function
import pygal
import os
import sys
def main():
    result = os.getenv('RESULT')
    print(result)
   if __name__ == "__main__":
   main()
END
}
plot_graph ${var[@]}

Arguments are being split and they are not being treated as a single variable.

争论正在分裂,他们没有被当作一个单一的变量来对待。

Out of result will be :[ {"a":"b", ] 

where as I want the entire var value to be read as a string and then I can split it into multiple dictionaries.

在这里,我希望将整个var值作为字符串读取,然后我可以将其拆分为多个字典。

Please help me get over this.

请帮我解决这个问题。

2 个解决方案

#1


0  

seems problem of plot_graph $var

似乎是plot_graph $var的问题。

The following code should work:

下面的代码应该工作:

var="({ 'a':'b', 'c':'d', 'e':'f'} { 'a1':'b1', 'c1':'d1', 'e1':'f1', 'g1':'h1' })"

echo $var



function plot_graph {
echo $1
RESULT="$1"   python - <<END
from __future__ import print_function
import os
import sys
def main():
    result = os.getenv('RESULT')
    print(result)
if __name__ == "__main__":
    main()
END
}
plot_graph "$var"

#2


0  

If I understand well, you want a concatenated dictionary with all keys. See how to concatenate two dictionaries to create a new one in Python?

如果我理解得很好,你需要一个带所有键的连接字典。看看如何将两个字典连接起来以在Python中创建一个新的字典?

#1


0  

seems problem of plot_graph $var

似乎是plot_graph $var的问题。

The following code should work:

下面的代码应该工作:

var="({ 'a':'b', 'c':'d', 'e':'f'} { 'a1':'b1', 'c1':'d1', 'e1':'f1', 'g1':'h1' })"

echo $var



function plot_graph {
echo $1
RESULT="$1"   python - <<END
from __future__ import print_function
import os
import sys
def main():
    result = os.getenv('RESULT')
    print(result)
if __name__ == "__main__":
    main()
END
}
plot_graph "$var"

#2


0  

If I understand well, you want a concatenated dictionary with all keys. See how to concatenate two dictionaries to create a new one in Python?

如果我理解得很好,你需要一个带所有键的连接字典。看看如何将两个字典连接起来以在Python中创建一个新的字典?