取决于先前inpu的乘法函数

2024-09-29 23:19:37 发布

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

我正在尝试编写一个小脚本来编写一个html文件,这个文件必须有一个包含两行的表,这两行有一些关于不同产品的信息,我设法做到了这一点,但现在我需要这个表根据以前的输入数据重复多次,因此,我想我可以乘的函数,其中包含的html代码,但它不工作,实际上我不太清楚我在这里做什么,所以一点点的帮助不会是坏事。。。你知道吗

这就是我想要的:

输入--->;多少表:3

所以html输出文件应该是这样的

<-- TABLE 1 -->
<table>
  <tr>
    <td colspan="4" height="30"></td>
  </tr>
  <tr>
    <td width="50" class="width6p"></td>
    <td width="260" class="width44p"><a href="http://www.site/site/product/CODEPRODUCT_1" target="_blank"><img src="http://site/image/CODEPRODUCT_1" width="230" alt="DESCRIPTION_1" style="display:block" border="0" class="width90p"/></a></td>
    <td width="30" class="width3p"></td>
    <td width="260" class="width44p"><a href="http://www.site/site/product/CODEPRODUCT_2" target="_blank"><img src="http://site/image/CODEPRODUCT_2" width="230" alt="DESCRIPTION_2" style="display:block" border="0" class="width90p"/></a></td>
  </tr>
  <tr>
    <td></td>
    <td class="font14" valign="top" style=" font-size: 16px; inline-height:0px; font-family:Helvetica, sans-serif; font-weight:lighter; color:#666666; line-height:130%; padding:10px 0px;">
      <span style="font-weight: bold; color:#008EAA" class="font14">DESCRIPTION_1</span ><br />
      <span style="font-weight: bold; color:#008EAA; font-size:14px;" class="font14"> DESCRIPTION_1</span><br/>
      <span style="font-size:12px;" class="font12">SKU: CODEPRODUCT_-1</span><br />
      <span style="font-size:18px;" class="font14">$ </span>
      <span style="font-size:24px; line-height:30px;" class="font20">PRICE_1</span>
      <span style="font-size:12px; text-transform: uppercase;" class="font10"> C/U</span> <br>
    </td>
    <td></td>
    <td class="font14" valign="top" style=" font-size: 16px; inline-height:0px; font-family:Helvetica, sans-serif; font-weight:lighter; color:#666666; line-height:130%; padding:10px 0px;">
      <span style="font-weight: bold; color:#008EAA" class="font14">DESCRIPTION_2</span><br />
      <span style="font-weight: bold; color:#008EAA; font-size:14px;" class="font14"> DESCRIPTION_2</span><br/>
      <span style="font-size:12px;" class="font12">SKU: CODEPRODUCT_-2</span><br />
      <span style="font-size:18px;" class="font14">$ </span>
      <span style="font-size:24px; line-height:30px;" class="font20">PRICE_2</span>
      <span style="font-size:12px; text-transform: uppercase;" class="font10"> C/U</span> <br>
    </td>
  </tr>
  <tr>
    <td></td>
    <td style="inline-height:0px;padding-top:4px;"><a href="http://www.site/site/product/CODEPRODUCT_1" target="_blank"><img src="http://www.site/images/loquiero_med.png" width="142" title="Ver producto" style="display:block" border="0" class="width115"/></a></td>
    <td></td>
    <td style="inline-height:0px;padding-top:4px;"><a href="http://www.site/site/product/CODEPRODUCT_2" target="_blank"><img src="http://www.site/images/loquiero_med.png" width="142" title="Ver producto" style="display:block" border="0" class="width115"/></a></td>
  </tr>
</table>
<-- TABLE 2 -->
<table>
  <tr>
    <td colspan="4" height="30"></td>
  </tr>
  <tr>
    <td width="50" class="width6p"></td>
    <td width="260" class="width44p"><a href="http://www.site/site/product/CODEPRODUCT_1" target="_blank"><img src="http://site/image/CODEPRODUCT_1" width="230" alt="DESCRIPTION_1" style="display:block" border="0" class="width90p"/></a></td>
    <td width="30" class="width3p"></td>
    <td width="260" class="width44p"><a href="http://www.site/site/product/CODEPRODUCT_2" target="_blank"><img src="http://site/image/CODEPRODUCT_2" width="230" alt="DESCRIPTION_2" style="display:block" border="0" class="width90p"/></a></td>
  </tr>
  <tr>
    <td></td>
    <td class="font14" valign="top" style=" font-size: 16px; inline-height:0px; font-family:Helvetica, sans-serif; font-weight:lighter; color:#666666; line-height:130%; padding:10px 0px;">
      <span style="font-weight: bold; color:#008EAA" class="font14">DESCRIPTION_1</span ><br />
      <span style="font-weight: bold; color:#008EAA; font-size:14px;" class="font14"> DESCRIPTION_1</span><br/>
      <span style="font-size:12px;" class="font12">SKU: CODEPRODUCT_-1</span><br />
      <span style="font-size:18px;" class="font14">$ </span>
      <span style="font-size:24px; line-height:30px;" class="font20">PRICE_1</span>
      <span style="font-size:12px; text-transform: uppercase;" class="font10"> C/U</span> <br>
    </td>
    <td></td>
    <td class="font14" valign="top" style=" font-size: 16px; inline-height:0px; font-family:Helvetica, sans-serif; font-weight:lighter; color:#666666; line-height:130%; padding:10px 0px;">
      <span style="font-weight: bold; color:#008EAA" class="font14">DESCRIPTION_2</span><br />
      <span style="font-weight: bold; color:#008EAA; font-size:14px;" class="font14"> DESCRIPTION_2</span><br/>
      <span style="font-size:12px;" class="font12">SKU: CODEPRODUCT_-2</span><br />
      <span style="font-size:18px;" class="font14">$ </span>
      <span style="font-size:24px; line-height:30px;" class="font20">PRICE_2</span>
      <span style="font-size:12px; text-transform: uppercase;" class="font10"> C/U</span> <br>
    </td>
  </tr>
  <tr>
    <td></td>
    <td style="inline-height:0px;padding-top:4px;"><a href="http://www.site/site/product/CODEPRODUCT_1" target="_blank"><img src="http://www.site/images/loquiero_med.png" width="142" title="Ver producto" style="display:block" border="0" class="width115"/></a></td>
    <td></td>
    <td style="inline-height:0px;padding-top:4px;"><a href="http://www.site/site/product/CODEPRODUCT_2" target="_blank"><img src="http://www.site/images/loquiero_med.png" width="142" title="Ver producto" style="display:block" border="0" class="width115"/></a></td>
  </tr>
</table>
<-- TABLE 3 -->
<table>
  <tr>
    <td colspan="4" height="30"></td>
  </tr>
  <tr>
    <td width="50" class="width6p"></td>
    <td width="260" class="width44p"><a href="http://www.site/site/product/CODEPRODUCT_1" target="_blank"><img src="http://site/image/CODEPRODUCT_1" width="230" alt="DESCRIPTION_1" style="display:block" border="0" class="width90p"/></a></td>
    <td width="30" class="width3p"></td>
    <td width="260" class="width44p"><a href="http://www.site/site/product/CODEPRODUCT_2" target="_blank"><img src="http://site/image/CODEPRODUCT_2" width="230" alt="DESCRIPTION_2" style="display:block" border="0" class="width90p"/></a></td>
  </tr>
  <tr>
    <td></td>
    <td class="font14" valign="top" style=" font-size: 16px; inline-height:0px; font-family:Helvetica, sans-serif; font-weight:lighter; color:#666666; line-height:130%; padding:10px 0px;">
      <span style="font-weight: bold; color:#008EAA" class="font14">DESCRIPTION_1</span ><br />
      <span style="font-weight: bold; color:#008EAA; font-size:14px;" class="font14"> DESCRIPTION_1</span><br/>
      <span style="font-size:12px;" class="font12">SKU: CODEPRODUCT_-1</span><br />
      <span style="font-size:18px;" class="font14">$ </span>
      <span style="font-size:24px; line-height:30px;" class="font20">PRICE_1</span>
      <span style="font-size:12px; text-transform: uppercase;" class="font10"> C/U</span> <br>
    </td>
    <td></td>
    <td class="font14" valign="top" style=" font-size: 16px; inline-height:0px; font-family:Helvetica, sans-serif; font-weight:lighter; color:#666666; line-height:130%; padding:10px 0px;">
      <span style="font-weight: bold; color:#008EAA" class="font14">DESCRIPTION_2</span><br />
      <span style="font-weight: bold; color:#008EAA; font-size:14px;" class="font14"> DESCRIPTION_2</span><br/>
      <span style="font-size:12px;" class="font12">SKU: CODEPRODUCT_-2</span><br />
      <span style="font-size:18px;" class="font14">$ </span>
      <span style="font-size:24px; line-height:30px;" class="font20">PRICE_2</span>
      <span style="font-size:12px; text-transform: uppercase;" class="font10"> C/U</span> <br>
    </td>
  </tr>
  <tr>
    <td></td>
    <td style="inline-height:0px;padding-top:4px;"><a href="http://www.site/site/product/CODEPRODUCT_1" target="_blank"><img src="http://www.site/images/loquiero_med.png" width="142" title="Ver producto" style="display:block" border="0" class="width115"/></a></td>
    <td></td>
    <td style="inline-height:0px;padding-top:4px;"><a href="http://www.site/site/product/CODEPRODUCT_2" target="_blank"><img src="http://www.site/images/loquiero_med.png" width="142" title="Ver producto" style="display:block" border="0" class="width115"/></a></td>
  </tr>
</table>

这是我的Python代码

import locale
import requests
import urlparse
import json

def html(sku_01,desc_01,sku_precio_1,sku_02,desc_02,sku_precio_2,bloque_prod):

  f = open('mkt-output.html','w')
  f.write(bloque_prod)
  f.close()

if __name__ == '__main__':
  sku_01 = raw_input('Ingrese SKU: ')
  desc_01 = raw_input('Descripcion de SKU: ')
  sku_precio_1 = raw_input('Precio de SKU: ')
  sku_02 = raw_input('Ingrese SKU: ')
  desc_02 = raw_input('Descripcion de SKU: ')
  sku_precio_2 = raw_input('Precio de SKU: ')
  bloque_prod = """<table>
  <tr>
    <td colspan="4" height="30"></td>
  </tr>
  <tr>
    <td width="50" class="width6p"></td>
    <td width="260" class="width44p"><a href="http://www.site/product/{}" target="_blank"><img src="http://site/images/{}" width="230" alt="{}" style="display:block" border="0" class="width90p"/></a></td>
    <td width="30" class="width3p"></td>
    <td width="260" class="width44p"><a href="http://www.site/product/{}" target="_blank"><img src="http://site/images/{}" width="230" alt="{}" style="display:block" border="0" class="width90p"/></a></td>
  </tr>
  <tr>
    <td></td>
    <td class="font14" valign="top" style=" font-size: 16px; inline-height:0px; font-family:Helvetica, sans-serif; font-weight:lighter; color:#666666; line-height:130%; padding:10px 0px;">
      <span style="font-weight: bold; color:#008EAA" class="font14">{}</span ><br />
      <span style="font-weight: bold; color:#008EAA; font-size:14px;" class="font14">{} {}</span><br/>
      <span style="font-size:12px;" class="font12">SKU: {}-{}</span><br />
      <span style="font-size:18px;" class="font14">$ </span>
      <span style="font-size:24px; line-height:30px;" class="font20">{}</span>
      <span style="font-size:12px; text-transform: uppercase;" class="font10"> C/U</span> <br>
    </td>
    <td></td>
    <td class="font14" valign="top" style=" font-size: 16px; inline-height:0px; font-family:Helvetica, sans-serif; font-weight:lighter; color:#666666; line-height:130%; padding:10px 0px;">
      <span style="font-weight: bold; color:#008EAA" class="font14">{}</span><br />
      <span style="font-weight: bold; color:#008EAA; font-size:14px;" class="font14">{} {}</span><br/>
      <span style="font-size:12px;" class="font12">SKU: {}-{}</span><br />
      <span style="font-size:18px;" class="font14">$ </span>
      <span style="font-size:24px; line-height:30px;" class="font20">{}</span>
      <span style="font-size:12px; text-transform: uppercase;" class="font10"> C/U</span> <br>
    </td>
  </tr>
  <tr>
    <td></td>
    <td style="inline-height:0px;padding-top:4px;"><a href="http://www.site/product/{}" target="_blank"><img src="http://www.site/templates/images/loquiero_med.png" width="142" title="Ver producto" style="display:block" border="0" class="width115"/></a></td>
    <td></td>
    <td style="inline-height:0px;padding-top:4px;"><a href="http://www.site/product/{}" target="_blank"><img src="http://www.site/templates/images/loquiero_med.png" width="142" title="Ver producto" style="display:block" border="0" class="width115"/></a></td>
  </tr>
</table>""".format(sku_01,
  sku_01,
  desc_01,
  sku_02,
  sku_02,
  desc_02,
  ' '.join(desc_01.split()[0:3]),
  ' '.join(desc_01.split()[3:-1]),
  desc_01.split()[-1],
  sku_01[0:-1],
  sku_01[-1],
  sku_precio_1,
  ' '.join(desc_02.split()[0:3]),
  ' '.join(desc_02.split()[3:-1]),
  desc_02.split()[-1],
  sku_02[0:-1],
  sku_02[-1],
  sku_precio_2,
  sku_01,
  sku_02)

html(sku_01, desc_01, sku_precio_1, sku_02, desc_02, sku_precio_2, bloque_prod)

Tags: brhttpsizestylewwwsitewidthtr
1条回答
网友
1楼 · 发布于 2024-09-29 23:19:37

如果需要将重复数据写入HTML文件,只需将html()函数write variable bloque_prod乘以:

def html(sku_01,desc_01,sku_precio_1,sku_02,desc_02,sku_precio_2,bloque_prod,tables):

  f = open('mkt-output.html','w')
  f.write(bloque_prod * tables)
  f.close()

注意为表的重复数添加了变量tables。你知道吗

然后,在__main()__中定义变量tables

tables = input('Tables: ')

…并将tables添加到调用html()的最后一行

html(sku_01, desc_01, sku_precio_1, sku_02, desc_02, sku_precio_2, bloque_prod,tables)

你想写不同的表格吗?你知道吗

此外,如果有必要对当前正在打印的表进行注释,您可以在中添加if语句:

def html(sku_01,desc_01,sku_precio_1,sku_02,desc_02,sku_precio_2,bloque_prod,tables):
  f = open('mkt-output.html','w')
  rawHTML = ""
  for table in range(0, tables):
        rawHTML += ("\n<  TABLE " + str(table) + "  >\n" + bloque_prod)
  f.write(rawHTML)
  f.close()

(如果希望编号从1开始,只需将str(table)更改为str(table + 1)

编辑您似乎希望创建具有不同值的表。我会重写程序这样做:

import locale
import requests
import urlparse
import json

def createTable(sku_01,desc_01,sku_precio_1,sku_02,desc_02,sku_precio_2):
  bloque_prod = """<table>
  <tr>
    <td colspan="4" height="30"></td>
  </tr>
  <tr>
    <td width="50" class="width6p"></td>
    <td width="260" class="width44p"><a href="http://www.site/product/{}" target="_blank"><img src="http://site/images/{}" width="230" alt="{}" style="display:block" border="0" class="width90p"/></a></td>
    <td width="30" class="width3p"></td>
    <td width="260" class="width44p"><a href="http://www.site/product/{}" target="_blank"><img src="http://site/images/{}" width="230" alt="{}" style="display:block" border="0" class="width90p"/></a></td>
  </tr>
  <tr>
    <td></td>
    <td class="font14" valign="top" style=" font-size: 16px; inline-height:0px; font-family:Helvetica, sans-serif; font-weight:lighter; color:#666666; line-height:130%; padding:10px 0px;">
      <span style="font-weight: bold; color:#008EAA" class="font14">{}</span ><br />
      <span style="font-weight: bold; color:#008EAA; font-size:14px;" class="font14">{} {}</span><br/>
      <span style="font-size:12px;" class="font12">SKU: {}-{}</span><br />
      <span style="font-size:18px;" class="font14">$ </span>
      <span style="font-size:24px; line-height:30px;" class="font20">{}</span>
      <span style="font-size:12px; text-transform: uppercase;" class="font10"> C/U</span> <br>
    </td>
    <td></td>
    <td class="font14" valign="top" style=" font-size: 16px; inline-height:0px; font-family:Helvetica, sans-serif; font-weight:lighter; color:#666666; line-height:130%; padding:10px 0px;">
      <span style="font-weight: bold; color:#008EAA" class="font14">{}</span><br />
      <span style="font-weight: bold; color:#008EAA; font-size:14px;" class="font14">{} {}</span><br/>
      <span style="font-size:12px;" class="font12">SKU: {}-{}</span><br />
      <span style="font-size:18px;" class="font14">$ </span>
      <span style="font-size:24px; line-height:30px;" class="font20">{}</span>
      <span style="font-size:12px; text-transform: uppercase;" class="font10"> C/U</span> <br>
    </td>
  </tr>
  <tr>
    <td></td>
    <td style="inline-height:0px;padding-top:4px;"><a href="http://www.site/product/{}" target="_blank"><img src="http://www.site/templates/images/loquiero_med.png" width="142" title="Ver producto" style="display:block" border="0" class="width115"/></a></td>
    <td></td>
    <td style="inline-height:0px;padding-top:4px;"><a href="http://www.site/product/{}" target="_blank"><img src="http://www.site/templates/images/loquiero_med.png" width="142" title="Ver producto" style="display:block" border="0" class="width115"/></a></td>
  </tr>
</table>""".format(sku_01,
  sku_01,
  desc_01,
  sku_02,
  sku_02,
  desc_02,
  ' '.join(desc_01.split()[0:3]),
  ' '.join(desc_01.split()[3:-1]),
  desc_01.split()[-1],
  sku_01[0:-1],
  sku_01[-1],
  sku_precio_1,
  ' '.join(desc_02.split()[0:3]),
  ' '.join(desc_02.split()[3:-1]),
  desc_02.split()[-1],
  sku_02[0:-1],
  sku_02[-1],
  sku_precio_2,
  sku_01,
  sku_02)
  return bloque_prod

if __name__ == "__main__":
  f = open('mkt-output.html','w+') # Open file in w+ mode so we can append to the end
  for table in range(0,input("Tables: ")):
        print (" Table "+str(table)+" ")
        sku_01 = raw_input('Ingrese SKU: ')
        desc_01 = raw_input('Descripcion de SKU: ')
        sku_precio_1 = raw_input('Precio de SKU: ')
        sku_02 = raw_input('Ingrese SKU: ')
        desc_02 = raw_input('Descripcion de SKU: ')
        sku_precio_2 = raw_input('Precio de SKU: ')
        f.write(createTable(sku_01,desc_01,sku_precio_1,sku_02,desc_02,sku_precio_2))
  f.close()

希望有帮助。你知道吗

相关问题 更多 >

    热门问题