首页 > 解决方案 > 将 scala 代码转换为 Python 面临长 html java 脚本嵌入字符串的问题

问题描述

基本上我想生成html和java脚本动态替换tr中的行并保存在文件中。下面是我的 scala html 字符串

"""
 <?xml version="1.0" ?>
           <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitionalt//EN"
               "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
           <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
           <script>
               function searchAllTable() {
           var input, filter, table, tr, td, i ;
           input = document.getElementById("Search");
           filter = input.value.toUpperCase();
           table = document.getElementById("jobTable");
           tr = table.getElementsByTagName("tr"),
           th = table.getElementsByTagName("th");

           // Loop through all table rows, and hide those who don't match the search query
           for (i = 1; i < tr.length; i++) {
                       tr[i].style.display = "none";
                       for(var j=0; j<th.length; j++){
                   td = tr[i].getElementsByTagName("td")[j];
                   if (td) {
                       if (td.innerHTML.toUpperCase().indexOf(filter.toUpperCase()) > -1)                               {
                           tr[i].style.display = "";
                           break;
                       }
                   }
               }
           }
               }</script>
           <head>
           <link rel="icon"
                 type="image/png"
                 href="https://www.jpmorganchase.com/content/dam/shared/logos/jpmc-logo-290x20px.png">
             <title>$fileNm </title>

             <style rel="stylesheet" type="text/css">
             body {
             font: normal small normal 16px/1.4 Verdana;
           }
            .sticky{
                   position: sticky;
                   top: 0px;
                   font-size: small;
           }
           table {
               counter-reset: rowNumber;
               /*table-layout: fixed;*/
               /*width: 150%;*/
               font-size: small;
           }

           table tr:not(:first-child){
               counter-increment: rowNumber;
           }

           table tr td:first-child::before {
               content: counter(rowNumber);
               min-width: 1em;
               margin-right: 0.5em;
           }
           th {
             font-size: small;
             font-weight: bold;
             text-align: center;
             background-color: #B4B4D9;
             top: 25px;  /** top and position=sticky properties are for freezing the header **/
             position:sticky;
             white-space: break-spaces;

           }
           .hoverTable{
                width:100%;
                border-collapse:collapse;
            }
            .hoverTable td{
                padding:7px; border:#4e95f4 1px solid;
            }

            /* Define the hover highlight color for the table row */
               .hoverTable tr:hover {
                     background-color: #ffa;
               }
           .label {
             color: navy;
             padding: 8px;
             font-family: Arial;
             font-weight: bold;
           }
           td {
             padding: 1px;
             /*white-space: nowrap*/ /** its just like html pre tag */
           }
           tr:nth-child(even) {background-color: #f2f2f2;}
           tr {
               overflow: hidden;
               height: 14px;
               white-space: pre;
           }
           </style>
           <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
           <meta name="author" content="Ram Ghadiyaram">
           |</head>
           <div><img src="https://content.presspage.com/uploads/2658/c800_logo-stackoverflow-square.jpg?98978" width="290" height="20" alt="test "/>
            </div>
            <div class="label">$header</div>
            <div class="sticky"><label class="label">
                   Search:<input type="text" id="Search" onkeyup="searchAllTable()" placeholder="Search across report">
               </label>
           </div>
           <table id='jobTable' style="border:1px dotted blue;" class="hoverTable">
                           <tr><th>Index</th>
                            ${headers.map(h => s"<th>${WordUtils.capitalize(escape(h))}</th>").mkString}
                           </tr>
                           ${rows.map { row => s"<tr><td/>${row.map { c => s"<td>${escape(c)}</td>" }.mkString}</tr>" }.mkString}
                       </table>
                       </html>
"""

这是将生成 html 代码的 html 模板

我想在 python 中使用相同的字符串并用 python 代码替换

 ${rows.map { row => s"<tr><td/>${row.map { c => s"<td>${escape(c)}</td>" }.mkString}</tr>" }.mkString}

问题:我不能声明上面的字符串来替换行。当我在 html 内容 ''' 上方使用 f''' 时,它会给出错误有没有办法在 python 中实现这一点?

标签: javascriptpythonhtmlscala

解决方案


推荐阅读