根据先前的输入乘以函数

时间:2017-12-31 22:40:22

标签: python function

我正在尝试编写一个写一个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)

1 个答案:

答案 0 :(得分:0)

如果您需要将重复数据写入HTML文件,您只需将html()函数写入变量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

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

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()

希望有所帮助。

相关问题