将 Python 数据列表转换为 bash 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26162394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert a Python Data list to a bash array
提问by altus
I have a bash script that is calling a python script like so:
我有一个 bash 脚本,它正在调用一个 python 脚本,如下所示:
OUTPUT=$(python /path/path/script.py attr attr attr);
The python script will return a data list like so:
python 脚本将返回一个数据列表,如下所示:
[item1, item2, item3]
How can I convert the $OUPUT variable which is a string of the return python data list into a bash array?
如何将作为返回 python 数据列表的字符串的 $OUTPUT 变量转换为 bash 数组?
I'd like to read each item in bash if possible.
如果可能,我想阅读 bash 中的每个项目。
回答by Cyrus
Add ()
and | tr -d '[],'
:
添加()
和| tr -d '[],'
:
OUTPUT=($(python /path/path/script.py attr attr attr | tr -d '[],'))
echo ${OUTPUT[0]}
echo ${OUTPUT[1]}
echo ${OUTPUT[2]}
echo ${OUTPUT[@]}
Output:
输出:
item1
item2
item3
item1 item2 item3
回答by alasimpara
You can make your script.py print a string that separates each item with spaces, which Bash will convert to an array, or you can use Bash to convert the return value of the python script into the format you want.
您可以让您的 script.py 打印一个字符串,用空格分隔每个项目,Bash 会将其转换为数组,或者您可以使用 Bash 将 python 脚本的返回值转换为您想要的格式。
If you chose to print a string from your script.py you can use the following python code:
如果您选择从 script.py 打印字符串,则可以使用以下 Python 代码:
returnList = [1, 2, 3]
returnStr = ''
for item in returnList:
returnStr += str(item)+' '
print(returnStr)
In this case, the output of the following bash script:
在这种情况下,以下 bash 脚本的输出:
OUTPUT=$(python /path/to/script.py)
echo $OUTPUT
for i in $OUTPUT;
do
echo $i
done
is:
是:
1 2 3
1
2
3
Hope this helps you.
希望这对你有帮助。
回答by l0b0
Bash declares arrays like this:
Bash 声明数组是这样的:
foo=(bar baz ban)
To convert space separated command output to an array you can therefore do this:
要将空格分隔的命令输出转换为数组,您可以执行以下操作:
foo=($(my_command))
And to convert a list to a space separated string is very easy in Python:
在 Python 中将列表转换为空格分隔的字符串非常容易:
' '.join(my_array)
If you print
that instead of the list itself you can therefore trivially convert it to an array.
如果您使用print
它而不是列表本身,则可以轻松地将其转换为数组。
回答by chepner
I would use a short Python wrapper to convert the string into something more parseable by bash
.
我会使用一个简短的 Python 包装器将字符串转换为更易于解析的字符串bash
。
# Proxy for script.py output that includes some potential bash pitfalls
python_output="['a b', 'c*', 6]"
# Let Python output each element of the list as a separate line;
# the only thing this cannot handle is multi-line elements; workarounds
# are possible, but not worth getting into unless necessary.
while read -r; do
OUTPUT+=("$REPLY")
done < <(python -c 'import ast, sys
print "\n".join(str(x) for x in ast.literal_eval(sys.argv[1]))
' "$python_output")
# Verify that each element was added to the array unscathed.
for e in "${OUTPUT[@]}"; do
echo "$e"
done
In bash
4, you can use the readarray
command to replace the while loop:
在bash
4,你可以使用readarray
命令替换while循环:
readarray -t OUTPUT < <(python -c ... )
回答by mickours
If you want to simply create a bash list from a python list in python, you can use this one-liner:
如果你想简单地从 python 中的 python 列表创建一个 bash 列表,你可以使用这个单行:
python -c 'print(" ".join([str(elem) for elem in [1, "l", 12]]))'
It gives you directly the list:
它直接为您提供列表:
1 l 12
Note that it requires to modify the python code instead of doing this from the external bash code.
请注意,它需要修改 python 代码,而不是从外部 bash 代码执行此操作。