Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何放置列表';在html中使用shell脚本的s值_Python_Html_Shell - Fatal编程技术网

Python 如何放置列表';在html中使用shell脚本的s值

Python 如何放置列表';在html中使用shell脚本的s值,python,html,shell,Python,Html,Shell,场景:我从python获得了包含一组值的列表变量,我想将这些列表值放在运行时生成的html文件中 testmail.sh **cat~/**test.html <html> <head> <title> My System information </title> </head> <body> <h1> My system information : </h1>

场景:我从python获得了包含一组值的列表变量,我想将这些列表值放在运行时生成的html文件中

testmail.sh

**cat~/**test.html

<html>

<head>
    <title>

    My System information

    </title>

</head>

<body>

<h1> My system information : </h1>

我的系统信息
我的系统信息:
对于“$@”//中的值,它包含从python接收的列表值

美元价值 完成


EOF

上面的代码位于testmail.sh中,它生成test.html并显示值


但我希望这些值以适当的格式以html格式放入正文中。。但它不起作用…

我假设您实际上是在向bash脚本发送管道,而不是使用“$@”参数,因为这没有意义。=)

示例输出:

$ echo -e "one\n two\n three" | bash foo.html 
<html>
<p>one</p>
<p>two</p>
<p>three</p>
</html>
$echo-e“一\n二\n三”| bash foo.html
一个

两个

执行此操作的Shell脚本:

$ cat foo.html 
cat <<END
<html>
END
while read LINE
do
echo "<p>${LINE}</p>"
done
cat <<END
</html>
END
$cat foo.html
像这样的猫

python script.py |
sed -e '1i\<ul>' -e 's%.*%<li>&</li>%' -e '$a\</ul>'
python script.py|
sed-e'1i\
    '-e's%.*
  • &
  • %'-e'$a\
'
。。。但最好还是在Python脚本中添加一个选项来生成HTML(ish)输出

如果您想从Python中驱动shell脚本,还可以用Python进行HTML格式化

import subprocess

pipe = subprocess.Popen("testmail.sh", stdin=subprocess.PIPE)
pipe.stdin.write('<ul>\n')
for item in correctList:
    pipe.stdin.write('<li>%s</li>\n' % item)
pipe.stdin.write('</ul>\n')
pipe.stdin.close()
导入子流程
pipe=subprocess.Popen(“testmail.sh”,stdin=subprocess.pipe)
pipe.stdin.write(“
    \n”) 对于列表中的项目: pipe.stdin.write(“
  • %s
  • \n”%item) pipe.stdin.write(“
\n”) 管道标准关闭()

这假设您的
testmail.sh
从标准输入读取其数据,但显然还不是这样,因此您需要稍微更改现有数据。

它不起作用。。。上述解决方案我不清楚。。我必须简单地打印html文件中的列表变量值,该文件是由shell脚本生成的。该变量来自另一个脚本。它是如何工作的?你创建了foo.html文件了吗?并试图将python的结果传递给它?我正在讲述完整的场景。。。。一个python脚本称为bc4jCheck.py,其中包含以下代码subprocess.call([“/bin/bash”,“/home/vikggupt/testmail.sh”]+correctList)#其中correctList是list变量。。现在在testmail.sh中,我需要两件事1。我想生成html文件,我想迭代这个列表变量,并将值放入html文件2中。我想将此html文件发送到特定服务器。。。对于这个发送html文件,我已经有了代码。。。但是对于第一个问题,我需要帮助…不要使用while循环,请尝试在$@中为i设置
;做回显“$i

”;完成
实际上,我在shell脚本中有一些用于发送邮件的代码。。这就是为什么我想在shell脚本中生成html文件,我正在讲述完整的场景。。。。一个python脚本称为bc4jCheck.py,其中包含以下代码subprocess.call([“/bin/bash”,“/home/vikggupt/testmail.sh”]+correctList)#其中correctList是list变量。。现在在testmail.sh中,我需要两件事1。我想生成html文件,我想迭代这个列表变量,并将值放入html文件2中。我想将此html文件发送到特定服务器。。。对于这个发送html文件,我已经有了代码。。。但对于第一个问题,我需要帮助……恐怕你的评论没有多大意义。如果你有一个新问题,发布一个新问题。
import subprocess

pipe = subprocess.Popen("testmail.sh", stdin=subprocess.PIPE)
pipe.stdin.write('<ul>\n')
for item in correctList:
    pipe.stdin.write('<li>%s</li>\n' % item)
pipe.stdin.write('</ul>\n')
pipe.stdin.close()