Python 2.7 查找并返回文本的特定区域
大家好,新年快乐!我有一个文本,我喜欢使用python脚本从特定字符串返回特定区域! 这是我的课文Python 2.7 查找并返回文本的特定区域,python-2.7,Python 2.7,大家好,新年快乐!我有一个文本,我喜欢使用python脚本从特定字符串返回特定区域! 这是我的课文 #!/bin/sh case "$1" in start) ulimit -s 1024 /usr/bin/oscam --config-dir /etc/tuxbox/config/oscam --daemon --pidfile /tmp/oscam.pid --restart 2 --utf8 ;; stop) kill `cat /tmp/oscam.pid
#!/bin/sh
case "$1" in
start)
ulimit -s 1024
/usr/bin/oscam --config-dir /etc/tuxbox/config/oscam --daemon --pidfile /tmp/oscam.pid --restart 2 --utf8
;;
stop)
kill `cat /tmp/oscam.pid` 2> /dev/null
;;
restart|reload)
$0 stop
sleep 1
$0 start
;;
version)
echo "svn8631"
;;
info)
echo "oscam svn8631"
;;
*)
echo "Usage: $0 start|stop|restart"
exit 1
;;
esac
exit 0
从上面的文本中,我需要创建一个python命令,该命令始终只返回在引号之间写入的oscam svn8631的内容!到目前为止,我唯一能做的就是使用此代码返回此文本的另一个区域
try:
f = open("/etc/init.d/softcam", "r")
content = f.read()
f.close()
except:
content = ""
contentInfo = content.split("\n")
if (content != ""):
for line in contentInfo:
if line.__contains__("usr/bin/"):
idex = line.index("n/")
line = line[(idex + 2):]
return line
这当然会返回usr/bin文本后面的内容,我需要info)后面的另一个区域。有人能帮我吗?我不知道如何让我的脚本读取我需要的特定区域!提前谢谢 使用您创建的
内容变量
“oscam svn8631”文本可通过以下方式提取:
for chunk in content.split(";;"):
if chunk.strip().startswith('info)'):
return chunk.split('"')[1]
分解bashcase
语句的自然方法是在“;”上拆分它。然后,我们识别info
部分(chunk.strip().startswith('info'))
)并选择该部分中的第一个带引号的字符串(chunk.split(“”)[1]
).我不理解你的问题,请写下代码的预期输出。输出必须是oscam svn8631,我的问题得到了回答,非常感谢你的贡献!我的朋友你是纯粹的天才!我从来没有想到一个块函数可以在“;”之间分割;;“零件!你没有创造我的一天,你创造了我的一年!!!向你鞠躬!