如何使用python读取文本文件并以html格式输出

2024-04-26 14:30:49 发布

您现在位置:Python中文网/ 问答频道 /正文

嘿,我在学python。我想读一个文件(内容.txt)并将文件内容附加到html中,html中包含表格格式的数据。你知道吗

我把X1,X2,X3作为名字,为了知道他们是否完成了任务,我写了YES/no/NA和评论。取而代之直接在代码中取名字我想把它写在内容.txt文件。你知道吗

# write-html-2-windows.py

import webbrowser

f = open('helloworld.html','w')

message = """<html>
<h1> <style>
table.GeneratedTable {
  width: 100%;
  background-color: #ffffff;
  border-collapse: collapse;
  border-width: 2px;
  border-color: #ffcc00;
  border-style: solid;
  color: #000000;
}

table.GeneratedTable td, table.GeneratedTable th {
  border-width: 2px;
  border-color: #ffcc00;
  border-style: solid;
  padding: 3px;
}

table.GeneratedTable thead {
  background-color: #ffcc00;
}
</style>
 </h1>
<body><table class="GeneratedTable">
  <thead>
    <tr>
      <th>name</th>
      <th> task </th>
      <th>comment </th>
    </tr>
  </thead>
  <tbody>
   <tr>
      <td>X1</td>
   <td> 
  <input type="radio" value ="YES"   >YES<br>
  <input type="radio"  value ="NO"  >NO<br>
  <input type="radio"  value ="NA"  >NA<br>

  <td><input type="text" maxlength="100" ><\td>

  <\td>

    </tr>
    <tr>
      <td>X2</td>
  <td> 
  <input type="radio" value ="YES"  >YES<br>
  <input type="radio"  value ="NO"  >NO<br>
  <input type="radio"  value ="NA"  >NA<br>

  <td><input type="text" maxlength="100" ><\td>

  <\td>
    </tr>
    <tr>
      <td>X3</td>
  <td> 
  <input type="radio" value ="YES"   >YES<br>
  <input type="radio"  value ="NO"  >NO<br>
  <input type="radio"  value ="NA"  >NA<br>

  <td><input type="text" maxlength="100" ><\td>

  <\td>
    </tr>
  </tbody>
</table>

</html>"""

f.write(message)
f.close()

webbrowser.open_new_tab('helloworld.html')

我想从一个叫做内容.txt对于内容中的每一个值都应该有任务和注释。请帮助我如何处理? 提前谢谢。你知道吗


Tags: nobr内容inputvaluehtmltypetable