0

继续我最初的问题:

从 python 程序获取输入的 shell 脚本

所以我的 python 程序的标准输出存储在我的 shell 脚本中的一个变量中。现在,我必须解析这个输出,以便获取最后 22 个字符的子字符串。这些是唯一对我重要的。不幸的是,没有办法真正识别这些最后的字符(“keyword=”等),这意味着我必须完全按照它们的位置来做到这一点

4

2 回答 2

1

如果需要字符串的最后 N 个字符,可以使用简单的切片:

>>> v="this is the stdout from my shell command stored in a variable"
>>> v[-22:]
'd stored in a variable'

返回最后 22 个字符。

于 2012-08-30T22:37:42.563 回答
1

假设您正在使用bash,您可以使用子字符串扩展:

key=$(python ...)   # From  your previous question

suffix=${key: -22}  # The space between : and - is important
于 2012-08-30T22:38:49.937 回答